2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25 #define MIN_BLOCK_SIZE 128
27 static inline DWORD block_size(DWORD block)
29 return MIN_BLOCK_SIZE << block;
32 void jsheap_init(jsheap_t *heap)
34 memset(heap, 0, sizeof(*heap));
35 list_init(&heap->custom_blocks);
38 void *jsheap_alloc(jsheap_t *heap, DWORD size)
43 if(!heap->block_cnt) {
45 heap->blocks = heap_alloc(sizeof(void*));
50 tmp = heap_alloc(block_size(0));
54 heap->blocks[0] = tmp;
58 if(heap->offset + size < block_size(heap->last_block)) {
59 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
64 if(size < block_size(heap->last_block+1)) {
65 if(heap->last_block+1 == heap->block_cnt) {
66 tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
72 tmp = heap_alloc(block_size(heap->block_cnt+1));
76 heap->blocks[heap->block_cnt++] = tmp;
80 return heap->blocks[heap->last_block];
83 list = heap_alloc(size + sizeof(struct list));
87 list_add_head(&heap->custom_blocks, list);
91 void jsheap_clear(jsheap_t *heap)
95 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
101 void jsheap_free(jsheap_t *heap)
107 for(i=0; i < heap->block_cnt; i++)
108 heap_free(heap->blocks[i]);
109 heap_free(heap->blocks);