add NULL and size=0 special processing to limited_* functions
@@ -13,6 +13,9 @@ void *limited_malloc(size_t size) {
if (size + allocated_mem > kMemoryLimit) {
return nullptr;
}
+ if (size == 0) {
+ return nullptr;
+ }
void* m = malloc(size);
if (m != nullptr) {
allocated_mem += size;
@@ -33,21 +36,25 @@ void limited_free(void *ptr) {
}
void *limited_realloc(void *ptr, size_t size) {
- if (allocated_len_map.find(ptr) == allocated_len_map.end()) {
+ if (ptr != NULL && allocated_len_map.find(ptr) == allocated_len_map.end()) {
abort();
}
+ if (ptr == NULL) {
+ return limited_malloc(size);
+ }
long delta = (long) size - allocated_len_map[ptr];
if (delta + allocated_mem > kMemoryLimit) {
return nullptr;
}
void* new_ptr = realloc(ptr, size);
- if (new_ptr == nullptr) {
+ if (size > 0 && new_ptr == nullptr) {
return nullptr;
}
allocated_mem += delta;
allocated_len_map.erase(ptr);
+ if (size > 0) {
allocated_len_map[new_ptr] = size;
-
+ }
return new_ptr;
}
