diff options
author | Jan Hutter <jhutter@hsr.ch> | 2005-11-10 13:42:38 +0000 |
---|---|---|
committer | Jan Hutter <jhutter@hsr.ch> | 2005-11-10 13:42:38 +0000 |
commit | 87e7f9d74064fec4f29556bc9f6b0140680f989a (patch) | |
tree | 6a14b38aff6d6b412c81d9c79966a486dec3344d /Source/charon/allocator.c | |
parent | b92be4f06d5129f68964f4f41202369d99f34db9 (diff) | |
download | strongswan-87e7f9d74064fec4f29556bc9f6b0140680f989a.tar.bz2 strongswan-87e7f9d74064fec4f29556bc9f6b0140680f989a.tar.xz |
- added clone functionality and chunk_t support
Diffstat (limited to 'Source/charon/allocator.c')
-rw-r--r-- | Source/charon/allocator.c | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/Source/charon/allocator.c b/Source/charon/allocator.c index 5ad9ac9ab..0a4f45937 100644 --- a/Source/charon/allocator.c +++ b/Source/charon/allocator.c @@ -158,7 +158,20 @@ static void *allocate_special(private_allocator_t *this,size_t bytes, char * fil static void * allocate(allocator_t *allocator,size_t bytes, char * file,int line) { private_allocator_t *this = (private_allocator_t *) allocator; - return (allocate_special(this,bytes, file,line,TRUE)); + return (this->allocate_special(this,bytes, file,line,TRUE)); +} + +/** + * Implements allocator_t's function allocate_as_chunk. + * See #allocator_s.allocate_as_chunk for description. + */ +static chunk_t allocate_as_chunk(allocator_t *allocator,size_t bytes, char * file,int line) +{ + private_allocator_t *this = (private_allocator_t *) allocator; + chunk_t new_chunk; + new_chunk.ptr = this->allocate_special(this,bytes, file,line,TRUE); + new_chunk.len = (new_chunk.ptr == NULL) ? 0 : bytes; + return new_chunk; } /* @@ -222,8 +235,32 @@ static void * reallocate(allocator_t *allocator, void * old, size_t bytes, char return NULL; } - memcpy(new_space,old,allocated_memory->info.size_of_memory); + + /* the smaller size is copied to avoid overflows */ + memcpy(new_space,old,(allocated_memory->info.size_of_memory < bytes) ? allocated_memory->info.size_of_memory : bytes); pthread_mutex_unlock(&(this->mutex)); + this->public.free_pointer(&(this->public),old); + + return new_space; +} + +static void * clone_bytes(allocator_t *allocator,void * to_clone, size_t bytes, char * file, int line) +{ + private_allocator_t *this = (private_allocator_t *) allocator; + + if (to_clone == NULL) + { + return NULL; + } + + void *new_space = this->allocate_special(this,bytes,file,line,TRUE); + + if (new_space == NULL) + { + return NULL; + } + + memcpy(new_space,to_clone,bytes); return new_space; } @@ -266,8 +303,10 @@ static void allocator_report_memory_leaks(allocator_t *allocator) */ static private_allocator_t allocator = { public: {allocate: allocate, + allocate_as_chunk: allocate_as_chunk, free_pointer: free_pointer, reallocate: reallocate, + clone_bytes : clone_bytes, report_memory_leaks: allocator_report_memory_leaks}, allocations: NULL, allocate_special : allocate_special, |