| OLD | NEW |
| 1 /* | 1 /* |
| 2 tre-mem.c - TRE memory allocator | 2 tre-mem.c - TRE memory allocator |
| 3 | 3 |
| 4 Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi> | 4 Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi> |
| 5 All rights reserved. | 5 All rights reserved. |
| 6 | 6 |
| 7 Redistribution and use in source and binary forms, with or without | 7 Redistribution and use in source and binary forms, with or without |
| 8 modification, are permitted provided that the following conditions | 8 modification, are permitted provided that the following conditions |
| 9 are met: | 9 are met: |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 #include "tre.h" | 42 #include "tre.h" |
| 43 | 43 |
| 44 /* | 44 /* |
| 45 This memory allocator is for allocating small memory blocks efficiently | 45 This memory allocator is for allocating small memory blocks efficiently |
| 46 in terms of memory overhead and execution speed. The allocated blocks | 46 in terms of memory overhead and execution speed. The allocated blocks |
| 47 cannot be freed individually, only all at once. There can be multiple | 47 cannot be freed individually, only all at once. There can be multiple |
| 48 allocators, though. | 48 allocators, though. |
| 49 */ | 49 */ |
| 50 | 50 |
| 51 /* Returns a new memory allocator or NULL if out of memory. */ | 51 /* Returns a new memory allocator or NULL if out of memory. */ |
| 52 tre_mem_t | 52 tre_mem_t tre_mem_new_impl(int provided, void* provided_block) { |
| 53 tre_mem_new_impl(int provided, void *provided_block) | |
| 54 { | |
| 55 tre_mem_t mem; | 53 tre_mem_t mem; |
| 56 if (provided) | 54 if (provided) { |
| 57 { | 55 mem = provided_block; |
| 58 mem = provided_block; | 56 memset(mem, 0, sizeof(*mem)); |
| 59 memset(mem, 0, sizeof(*mem)); | 57 } else |
| 60 } | |
| 61 else | |
| 62 mem = xcalloc(1, sizeof(*mem)); | 58 mem = xcalloc(1, sizeof(*mem)); |
| 63 if (mem == NULL) | 59 if (mem == NULL) |
| 64 return NULL; | 60 return NULL; |
| 65 return mem; | 61 return mem; |
| 66 } | 62 } |
| 67 | 63 |
| 68 | |
| 69 /* Frees the memory allocator and all memory allocated with it. */ | 64 /* Frees the memory allocator and all memory allocated with it. */ |
| 70 void | 65 void tre_mem_destroy(tre_mem_t mem) { |
| 71 tre_mem_destroy(tre_mem_t mem) | |
| 72 { | |
| 73 tre_list_t *tmp, *l = mem->blocks; | 66 tre_list_t *tmp, *l = mem->blocks; |
| 74 | 67 |
| 75 while (l != NULL) | 68 while (l != NULL) { |
| 76 { | 69 xfree(l->data); |
| 77 xfree(l->data); | 70 tmp = l->next; |
| 78 tmp = l->next; | 71 xfree(l); |
| 79 xfree(l); | 72 l = tmp; |
| 80 l = tmp; | 73 } |
| 81 } | |
| 82 xfree(mem); | 74 xfree(mem); |
| 83 } | 75 } |
| 84 | 76 |
| 85 | |
| 86 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the | 77 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the |
| 87 allocated block or NULL if an underlying malloc() failed. */ | 78 allocated block or NULL if an underlying malloc() failed. */ |
| 88 void * | 79 void* tre_mem_alloc_impl(tre_mem_t mem, |
| 89 tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block, | 80 int provided, |
| 90 » » int zero, size_t size) | 81 void* provided_block, |
| 91 { | 82 int zero, |
| 92 void *ptr; | 83 size_t size) { |
| 84 void* ptr; |
| 93 | 85 |
| 94 if (mem->failed) | 86 if (mem->failed) { |
| 95 { | 87 return NULL; |
| 96 return NULL; | 88 } |
| 89 |
| 90 if (mem->n < size) { |
| 91 /* We need more memory than is available in the current block. |
| 92 Allocate a new block. */ |
| 93 tre_list_t* l; |
| 94 if (provided) { |
| 95 if (provided_block == NULL) { |
| 96 mem->failed = 1; |
| 97 return NULL; |
| 98 } |
| 99 mem->ptr = provided_block; |
| 100 mem->n = TRE_MEM_BLOCK_SIZE; |
| 101 } else { |
| 102 int block_size; |
| 103 if (size * 8 > TRE_MEM_BLOCK_SIZE) |
| 104 block_size = size * 8; |
| 105 else |
| 106 block_size = TRE_MEM_BLOCK_SIZE; |
| 107 l = xmalloc(sizeof(*l)); |
| 108 if (l == NULL) { |
| 109 mem->failed = 1; |
| 110 return NULL; |
| 111 } |
| 112 l->data = xmalloc(block_size); |
| 113 if (l->data == NULL) { |
| 114 xfree(l); |
| 115 mem->failed = 1; |
| 116 return NULL; |
| 117 } |
| 118 l->next = NULL; |
| 119 if (mem->current != NULL) |
| 120 mem->current->next = l; |
| 121 if (mem->blocks == NULL) |
| 122 mem->blocks = l; |
| 123 mem->current = l; |
| 124 mem->ptr = l->data; |
| 125 mem->n = block_size; |
| 97 } | 126 } |
| 98 | 127 } |
| 99 if (mem->n < size) | |
| 100 { | |
| 101 /* We need more memory than is available in the current block. | |
| 102 » Allocate a new block. */ | |
| 103 tre_list_t *l; | |
| 104 if (provided) | |
| 105 » { | |
| 106 » if (provided_block == NULL) | |
| 107 » { | |
| 108 » mem->failed = 1; | |
| 109 » return NULL; | |
| 110 » } | |
| 111 » mem->ptr = provided_block; | |
| 112 » mem->n = TRE_MEM_BLOCK_SIZE; | |
| 113 » } | |
| 114 else | |
| 115 » { | |
| 116 » int block_size; | |
| 117 » if (size * 8 > TRE_MEM_BLOCK_SIZE) | |
| 118 » block_size = size * 8; | |
| 119 » else | |
| 120 » block_size = TRE_MEM_BLOCK_SIZE; | |
| 121 » l = xmalloc(sizeof(*l)); | |
| 122 » if (l == NULL) | |
| 123 » { | |
| 124 » mem->failed = 1; | |
| 125 » return NULL; | |
| 126 » } | |
| 127 » l->data = xmalloc(block_size); | |
| 128 » if (l->data == NULL) | |
| 129 » { | |
| 130 » xfree(l); | |
| 131 » mem->failed = 1; | |
| 132 » return NULL; | |
| 133 » } | |
| 134 » l->next = NULL; | |
| 135 » if (mem->current != NULL) | |
| 136 » mem->current->next = l; | |
| 137 » if (mem->blocks == NULL) | |
| 138 » mem->blocks = l; | |
| 139 » mem->current = l; | |
| 140 » mem->ptr = l->data; | |
| 141 » mem->n = block_size; | |
| 142 » } | |
| 143 } | |
| 144 | 128 |
| 145 /* Make sure the next pointer will be aligned. */ | 129 /* Make sure the next pointer will be aligned. */ |
| 146 size += ALIGN(mem->ptr + size, long); | 130 size += ALIGN(mem->ptr + size, long); |
| 147 | 131 |
| 148 /* Allocate from current block. */ | 132 /* Allocate from current block. */ |
| 149 ptr = mem->ptr; | 133 ptr = mem->ptr; |
| 150 mem->ptr += size; | 134 mem->ptr += size; |
| 151 mem->n -= size; | 135 mem->n -= size; |
| 152 | 136 |
| 153 /* Set to zero if needed. */ | 137 /* Set to zero if needed. */ |
| 154 if (zero) | 138 if (zero) |
| 155 memset(ptr, 0, size); | 139 memset(ptr, 0, size); |
| 156 | 140 |
| 157 return ptr; | 141 return ptr; |
| 158 } | 142 } |
| OLD | NEW |