1 /* $Id: dopen.c,v 1.9 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include <sys/types.h>
35 static void common_init (Dict_BFile bf, int block_size, int cache)
39 bf->block_size = block_size;
44 bf->hits = bf->misses = 0;
46 /* Allocate all blocks in one chunk. */
47 bf->all_data = xmalloc (block_size * cache);
49 /* Allocate and initialize hash array (as empty) */
50 bf->hash_array = (struct Dict_file_block **)
51 xmalloc(sizeof(*bf->hash_array) * bf->hash_size);
52 for (i=bf->hash_size; --i >= 0; )
53 bf->hash_array[i] = NULL;
55 /* Allocate all block descriptors in one chunk */
56 bf->all_blocks = (struct Dict_file_block *)
57 xmalloc (sizeof(*bf->all_blocks) * cache);
59 /* Initialize the free list */
60 bf->free_list = bf->all_blocks;
61 for (i=0; i<cache-1; i++)
62 bf->all_blocks[i].h_next = bf->all_blocks+(i+1);
63 bf->all_blocks[i].h_next = NULL;
65 /* Initialize the data for each block. Will never be moved again */
66 for (i=0; i<cache; i++)
67 bf->all_blocks[i].data = (char*) bf->all_data + i*block_size;
69 /* Initialize lru queue */
75 Dict_BFile dict_bf_open (BFiles bfs, const char *name, int block_size,
80 dbf = (Dict_BFile) xmalloc (sizeof(*dbf));
81 dbf->bf = bf_open (bfs, name, block_size, rw);
84 common_init (dbf, block_size, cache);
88 void dict_bf_compact (Dict_BFile dbf)
90 dbf->compact_flag = 1;