ossp-pkg/act/act_grid.c
1.2
/*
** Act - Abstract Container Type Library
** Copyright (c) 1999-2000 Ralf S. Engelschall <rse@engelschall.com>
**
** This file is part of Act, a library for dealing with Abstract
** Container Types which can be found at http://www.ossp.org/pkg/act/.
**
** Permission to use, copy, modify, and distribute this software for
** any purpose with or without fee is hereby granted, provided that
** the above copyright notice and this permission notice appear in all
** copies.
**
** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
**
** act_grid.c: memory grid (implementation)
*/
#include "act_p.h"
#include "act_grid.h"
/* the top-level structure of a grid */
struct act_grid_st {
act_ctx_t *g_ctx;
void *g_seg_block;
void *g_seg_free;
size_t g_seg_num;
size_t g_seg_size;
};
act_grid_t *
act_grid_create(
act_ctx_t *ctx,
size_t segnum,
size_t segsize)
{
act_grid_t *grid = NULL;
void *block;
void *seg;
int i;
/* consistency checks */
insist(ctx != NULL, NULL);
insist(segnum >= 1, NULL);
insist(segsize >= sizeof(void *), NULL);
/* align segment size */
segsize = act_mem_align(segsize);
/* allocate memory chunks */
if ((grid = (act_grid_t *)act_mem_alloc_ctx(ctx, sizeof(act_grid_t))) == NULL)
return NULL;
if ((block = act_mem_alloc_ctx(ctx, segnum * segsize)) == NULL) {
act_mem_free_ctx(ctx, grid);
return NULL;
}
/* initialize top-level structure */
grid->g_ctx = act_ctx_dup(ctx, NULL);
grid->g_seg_block = block;
grid->g_seg_free = block;
grid->g_seg_num = segnum;
grid->g_seg_size = segsize;
/* initialize segment block */
seg = grid->g_seg_free;
for (i = 0; i < segnum-1; i++) {
*((void **)seg) = (char *)seg+segsize;
seg = (char *)seg+segsize;
}
*((void **)seg) = NULL;
return grid;
}
int
act_grid_destroy(
act_grid_t *grid)
{
act_ctx_t *ctx;
insist(grid != NULL, FALSE);
ctx = grid->g_ctx;
act_mem_free_ctx(ctx, grid->g_seg_block);
act_mem_free_ctx(ctx, grid);
return TRUE;
}
void *
act_grid_alloc(
act_grid_t *grid)
{
void *chunk = NULL;
insist(grid != NULL, NULL);
/* XXX */
return chunk;
}
int
act_grid_free(
act_grid_t *grid,
void *segptr)
{
insist(grid != NULL, FALSE);
insist(segptr != NULL, FALSE);
/* XXX */
return TRUE;
}