~ubuntu-branches/ubuntu/natty/freecell-solver/natty

« back to all changes in this revision

Viewing changes to alloc.c

  • Committer: Bazaar Package Importer
  • Author(s): RISKO Gergely
  • Date: 2009-07-15 17:42:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090715174219-2rlyyvse0kezacly
Tags: 2.34.0-1
New upstream version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2000 Shlomi Fish
 
2
 *
 
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
 
10
 * conditions:
 
11
 *
 
12
 * The above copyright notice and this permission notice shall be
 
13
 * included in all copies or substantial portions of the Software.
 
14
 *
 
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.
 
23
 */
1
24
/*
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.
5
 
 *
6
 
 * Written by Shlomi Fish ( http://www.shlomifish.org/ ), 2002
7
 
 *
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
 
27
 * last allocation.
9
28
 */
10
29
 
 
30
#define BUILDING_DLL 1
 
31
 
11
32
#include <stdlib.h>
12
33
#include <stdio.h>
13
34
 
15
36
 
16
37
#include "alloc.h"
17
38
 
18
 
#ifdef DMALLOC
19
 
#include "dmalloc.h"
20
 
#endif
21
 
 
22
 
#define ALLOCED_SIZE (8*1024-10*sizeof(char *))
23
 
 
24
 
fcs_compact_allocator_t *
25
 
    fc_solve_compact_allocator_new(void)
 
39
#define ALLOCED_SIZE (64*1024-(256+128))
 
40
 
 
41
void fc_solve_compact_allocator_init(
 
42
    fcs_compact_allocator_t * allocator
 
43
    )
26
44
{
27
 
    fcs_compact_allocator_t * allocator;
28
 
 
29
 
 
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 =
35
48
        (allocator->ptr =
37
50
        allocator->packs[0] =
38
51
        malloc(ALLOCED_SIZE))
39
52
            + ALLOCED_SIZE;
40
 
 
41
 
    return allocator;
 
53
    return;
42
54
}
43
55
 
44
56
void fc_solve_compact_allocator_extend(
46
58
        )
47
59
{
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)))
50
62
    {
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(
 
64
            allocator->packs,
 
65
            sizeof(allocator->packs[0]) * 
 
66
                ((allocator->num_packs) + IA_STATE_PACKS_GROW_BY)
 
67
        );
53
68
    }
54
69
 
55
70
    allocator->max_ptr =
56
71
        (allocator->ptr =
57
72
        allocator->rollback_ptr =
58
 
        allocator->packs[allocator->num_packs++] =
 
73
        allocator->packs[allocator->num_packs-1] =
59
74
        malloc(ALLOCED_SIZE))
60
75
            + ALLOCED_SIZE;
61
76
}
62
77
 
63
 
#if 0
64
 
char *
65
 
    fc_solve_compact_allocator_alloc(
66
 
        fcs_compact_allocator_t * allocator,
67
 
        int how_much
68
 
            )
69
 
{
70
 
    if (allocator->max_ptr - allocator->ptr < how_much)
71
 
    {
72
 
        fc_solve_compact_allocator_extend(allocator);
73
 
    }
74
 
    allocator->rollback_ptr = allocator->ptr;
75
 
    allocator->ptr += (how_much+(4-(how_much&0x3)));
76
 
    return allocator->rollback_ptr;
77
 
}
78
 
 
79
 
void fc_solve_compact_allocator_release(fcs_compact_allocator_t * allocator)
80
 
{
81
 
    allocator->ptr = allocator->rollback_ptr;
82
 
}
83
 
#endif
84
78
 
85
79
void fc_solve_compact_allocator_finish(fcs_compact_allocator_t * allocator)
86
80
{
90
84
        free(allocator->packs[a]);
91
85
    }
92
86
    free(allocator->packs);
93
 
    free(allocator);
94
 
}
95
 
 
96
 
void fc_solve_compact_allocator_foreach(
97
 
    fcs_compact_allocator_t * allocator,
98
 
    int data_width,
99
 
    void (*ptr_function)(void *, void *),
100
 
    void * context
101
 
        )
102
 
{
103
 
    int pack;
104
 
    char * ptr, * max_ptr;
105
 
    for(pack=0;pack<allocator->num_packs-1;pack++)
106
 
    {
107
 
        ptr = allocator->packs[pack];
108
 
        max_ptr = ptr + ALLOCED_SIZE - data_width;
109
 
        while (ptr <= max_ptr)
110
 
        {
111
 
            ptr_function(ptr, context);
112
 
            ptr += data_width;
113
 
        }
114
 
    }
115
 
    /* Run the callback on the last pack */
116
 
    ptr = allocator->packs[pack];
117
 
    max_ptr = allocator->rollback_ptr;
118
 
    while (ptr <= max_ptr)
119
 
    {
120
 
        ptr_function(ptr, context);
121
 
        ptr += data_width;
122
 
    }
123
 
}
124
 
 
125
 
 
126
 
 
127
 
 
 
87
}