1
/* Copyright (c) 2000 Shlomi Fish
3
* Permission is hereby granted, free of charge, to any person
4
* obtaining a copy of this software and associated documentation
5
* files (the "Software"), to deal in the Software without
6
* restriction, including without limitation the rights to use,
7
* copy, modify, merge, publish, distribute, sublicense, and/or sell
8
* copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following
12
* The above copyright notice and this permission notice shall be
13
* included in all copies or substantial portions of the Software.
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
* OTHER DEALINGS IN THE SOFTWARE.
2
* alloc.c - a dynamic memory allocator. It allocates blocks of relatively
3
* small size, in a contiguous, compact manner. The most recent block can
4
* be released, but otherwise the blocks are kept for prosperity.
6
* Written by Shlomi Fish ( http://www.shlomifish.org/ ), 2002
8
* This file is in the public domain (it's uncopyrighted).
25
* alloc.c - the Freecell Solver compact allocator. Used to allocate
26
* columns and other small allocations of a short size. Is able to revert the
30
#define BUILDING_DLL 1
11
32
#include <stdlib.h>
22
#define ALLOCED_SIZE (8*1024-10*sizeof(char *))
24
fcs_compact_allocator_t *
25
fc_solve_compact_allocator_new(void)
39
#define ALLOCED_SIZE (64*1024-(256+128))
41
void fc_solve_compact_allocator_init(
42
fcs_compact_allocator_t * allocator
27
fcs_compact_allocator_t * allocator;
30
allocator = (fcs_compact_allocator_t *)malloc(sizeof(*allocator));
31
allocator->max_num_packs = IA_STATE_PACKS_GROW_BY;
32
allocator->packs = (char * *)malloc(sizeof(allocator->packs[0]) * allocator->max_num_packs);
45
allocator->packs = (char * *)malloc(sizeof(allocator->packs[0]) * IA_STATE_PACKS_GROW_BY);
33
46
allocator->num_packs = 1;
34
47
allocator->max_ptr =
48
60
/* Allocate a new pack */
49
if (allocator->num_packs == allocator->max_num_packs)
61
if (! ((++allocator->num_packs) & (IA_STATE_PACKS_GROW_BY-1)))
51
allocator->max_num_packs += IA_STATE_PACKS_GROW_BY;
52
allocator->packs = (char * *)realloc(allocator->packs, sizeof(allocator->packs[0]) * allocator->max_num_packs);
63
allocator->packs = (char * *)realloc(
65
sizeof(allocator->packs[0]) *
66
((allocator->num_packs) + IA_STATE_PACKS_GROW_BY)
55
70
allocator->max_ptr =
57
72
allocator->rollback_ptr =
58
allocator->packs[allocator->num_packs++] =
73
allocator->packs[allocator->num_packs-1] =
59
74
malloc(ALLOCED_SIZE))
65
fc_solve_compact_allocator_alloc(
66
fcs_compact_allocator_t * allocator,
70
if (allocator->max_ptr - allocator->ptr < how_much)
72
fc_solve_compact_allocator_extend(allocator);
74
allocator->rollback_ptr = allocator->ptr;
75
allocator->ptr += (how_much+(4-(how_much&0x3)));
76
return allocator->rollback_ptr;
79
void fc_solve_compact_allocator_release(fcs_compact_allocator_t * allocator)
81
allocator->ptr = allocator->rollback_ptr;
85
79
void fc_solve_compact_allocator_finish(fcs_compact_allocator_t * allocator)
90
84
free(allocator->packs[a]);
92
86
free(allocator->packs);
96
void fc_solve_compact_allocator_foreach(
97
fcs_compact_allocator_t * allocator,
99
void (*ptr_function)(void *, void *),
104
char * ptr, * max_ptr;
105
for(pack=0;pack<allocator->num_packs-1;pack++)
107
ptr = allocator->packs[pack];
108
max_ptr = ptr + ALLOCED_SIZE - data_width;
109
while (ptr <= max_ptr)
111
ptr_function(ptr, context);
115
/* Run the callback on the last pack */
116
ptr = allocator->packs[pack];
117
max_ptr = allocator->rollback_ptr;
118
while (ptr <= max_ptr)
120
ptr_function(ptr, context);