| 39 | } |
| 40 | |
| 41 | void *JsonAllocator::allocate(size_t size) { |
| 42 | size = (size + 7) & ~7; |
| 43 | |
| 44 | if (head && head->used + size <= JSON_ZONE_SIZE) { |
| 45 | char *p = (char *)head + head->used; |
| 46 | head->used += size; |
| 47 | return p; |
| 48 | } |
| 49 | |
| 50 | size_t allocSize = sizeof(Zone) + size; |
| 51 | Zone *zone = (Zone *)malloc(allocSize <= JSON_ZONE_SIZE ? JSON_ZONE_SIZE : allocSize); |
| 52 | if (zone == nullptr) |
| 53 | return nullptr; |
| 54 | zone->used = allocSize; |
| 55 | if (allocSize <= JSON_ZONE_SIZE || head == nullptr) { |
| 56 | zone->next = head; |
| 57 | head = zone; |
| 58 | } else { |
| 59 | zone->next = head->next; |
| 60 | head->next = zone; |
| 61 | } |
| 62 | return (char *)zone + sizeof(Zone); |
| 63 | } |
| 64 | |
| 65 | void JsonAllocator::deallocate() { |
| 66 | while (head) { |