| 60 | |
| 61 | |
| 62 | void *dObStack::alloc (int num_bytes) |
| 63 | { |
| 64 | if ((size_t)num_bytes > MAX_ALLOC_SIZE) dDebug (0,"num_bytes too large"); |
| 65 | |
| 66 | // allocate or move to a new arena if necessary |
| 67 | if (!first) { |
| 68 | // allocate the first arena if necessary |
| 69 | first = last = (Arena *) dAlloc (dOBSTACK_ARENA_SIZE); |
| 70 | first->next = 0; |
| 71 | first->used = sizeof (Arena); |
| 72 | ROUND_UP_OFFSET_TO_EFFICIENT_SIZE (first,first->used); |
| 73 | } |
| 74 | else { |
| 75 | // we already have one or more arenas, see if a new arena must be used |
| 76 | if ((last->used + num_bytes) > dOBSTACK_ARENA_SIZE) { |
| 77 | if (!last->next) { |
| 78 | last->next = (Arena *) dAlloc (dOBSTACK_ARENA_SIZE); |
| 79 | last->next->next = 0; |
| 80 | } |
| 81 | last = last->next; |
| 82 | last->used = sizeof (Arena); |
| 83 | ROUND_UP_OFFSET_TO_EFFICIENT_SIZE (last,last->used); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // allocate an area in the arena |
| 88 | char *c = ((char*) last) + last->used; |
| 89 | last->used += num_bytes; |
| 90 | ROUND_UP_OFFSET_TO_EFFICIENT_SIZE (last,last->used); |
| 91 | return c; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void dObStack::freeAll() |
no test coverage detected