IDZEBRA  2.1.2
dopen.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 */
19 
20 
21 
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 #include "dict-p.h"
34 
35 static void common_init(Dict_BFile bf, int block_size, int cache)
36 {
37  int i;
38 
39  bf->block_size = block_size;
40  bf->compact_flag = 0;
41  bf->cache = cache;
42  bf->hash_size = 31;
43 
44  bf->hits = bf->misses = 0;
45 
46  /* Allocate all blocks in one chunk. */
47  bf->all_data = xmalloc(block_size * cache);
48 
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;
54 
55  /* Allocate all block descriptors in one chunk */
56  bf->all_blocks = (struct Dict_file_block *)
57  xmalloc(sizeof(*bf->all_blocks) * cache);
58 
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;
64 
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;
68 
69  /* Initialize lru queue */
70  bf->lru_back = NULL;
71  bf->lru_front = NULL;
72 }
73 
74 
75 Dict_BFile dict_bf_open(BFiles bfs, const char *name, int block_size,
76  int cache, int rw)
77 {
78  Dict_BFile dbf;
79 
80  dbf = (Dict_BFile) xmalloc(sizeof(*dbf));
81  dbf->bf = bf_open(bfs, name, block_size, rw);
82  if (!dbf->bf)
83  {
84  xfree(dbf);
85  return 0;
86  }
87  common_init(dbf, block_size, cache);
88  return dbf;
89 }
90 
92 {
93  dbf->compact_flag = 1;
94 }
95 /*
96  * Local variables:
97  * c-basic-offset: 4
98  * c-file-style: "Stroustrup"
99  * indent-tabs-mode: nil
100  * End:
101  * vim: shiftwidth=4 tabstop=8 expandtab
102  */
103 
struct Dict_file_block * all_blocks
Definition: dict-p.h:58
int compact_flag
Definition: dict-p.h:69
void dict_bf_compact(Dict_BFile dbf)
Definition: dopen.c:91
Dict_BFile dict_bf_open(BFiles bfs, const char *name, int block_size, int cache, int rw)
Definition: dopen.c:75
void * all_data
Definition: dict-p.h:64
struct Dict_file_struct * Dict_BFile
int block_size
Definition: dict-p.h:66
static void common_init(Dict_BFile bf, int block_size, int cache)
Definition: dopen.c:35
struct Dict_file_block * free_list
Definition: dict-p.h:59
struct Dict_file_block * lru_front
Definition: dict-p.h:62
struct Dict_file_block * h_next
Definition: dict-p.h:45
void * data
Definition: dict-p.h:47
struct Dict_file_block * lru_back
Definition: dict-p.h:62
struct Dict_file_block ** hash_array
Definition: dict-p.h:60
BFile bf_open(BFiles bfs, const char *name, int block_size, int wflag)
opens and returns a Block file handle
Definition: bfile.c:261