Changeset fdc190f in mainline
- Timestamp:
- 2018-11-13T16:48:08Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- bcd4dd4
- Parents:
- 71fb5ac
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-11-13 16:44:03)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-11-13 16:48:08)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/mm/malloc.c
r71fb5ac rfdc190f 82 82 } 83 83 84 static inline bool _is_pow2(size_t x) 85 { 86 return (x & (x - 1)) == 0; 87 } 88 84 89 static void _check_sizes(size_t *alignment, size_t *size) 85 90 { … … 87 92 assert(alignment); 88 93 89 assert(*size > 0); 94 /* Force size to be nonzero. */ 95 if (*size == 0) 96 *size = 1; 90 97 91 98 /* Alignment must be a power of 2. */ 92 assert(_ _builtin_popcountl(*alignment) <= 1);99 assert(_is_pow2(*alignment)); 93 100 assert(*alignment <= PAGE_SIZE); 94 101 … … 132 139 /* We assume that slab objects are aligned naturally */ 133 140 return slab_alloc(cache_for_size(size), FRAME_ATOMIC); 141 } 142 143 static void *mem_realloc(void *old_ptr, size_t alignment, size_t old_size, 144 size_t new_size) 145 { 146 assert(old_ptr); 147 _check_sizes(&alignment, &old_size); 148 _check_sizes(&alignment, &new_size); 149 150 // TODO: handle big objects 151 assert(new_size <= (1 << SLAB_MAX_MALLOC_W)); 152 153 slab_cache_t *old_cache = cache_for_size(old_size); 154 slab_cache_t *new_cache = cache_for_size(new_size); 155 if (old_cache == new_cache) 156 return old_ptr; 157 158 void *new_ptr = slab_alloc(new_cache, FRAME_ATOMIC); 159 if (!new_ptr) 160 return NULL; 161 162 memcpy(new_ptr, old_ptr, min(old_size, new_size)); 163 slab_free(old_cache, old_ptr); 164 return new_ptr; 134 165 } 135 166 … … 186 217 size_t old_size = ((size_t *) old_obj)[-1]; 187 218 188 if (cache_for_size(old_size + _offset) == 189 cache_for_size(new_size + _offset)) 190 return old_obj; 191 192 void *new_obj = malloc(new_size); 219 void *new_obj = mem_realloc(old_obj - _offset, alignof(max_align_t), 220 old_size + _offset, new_size + _offset) + _offset; 193 221 if (!new_obj) 194 222 return NULL; 195 223 196 memcpy(new_obj, old_obj, min(old_size, new_size)); 197 free(old_obj); 224 ((size_t *) new_obj)[-1] = new_size; 198 225 return new_obj; 199 226 }
Note:
See TracChangeset
for help on using the changeset viewer.