comparison cdb.c @ 12:0e21568dec72

allocate a Cdb struct rather than using a single static one, note that cdb_free does _not_ call alloc_free, just releases the map, in fact alloc_free is not called anywhere!
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Mon, 03 Feb 2025 17:31:23 +0000
parents 41b9b6a160d1
children 70f4df5a7283
comparison
equal deleted inserted replaced
11:978eaa166609 12:0e21568dec72
5 #include <sys/mman.h> 5 #include <sys/mman.h>
6 #include "readwrite.h" 6 #include "readwrite.h"
7 #include "error.h" 7 #include "error.h"
8 #include "seek.h" 8 #include "seek.h"
9 #include "byte.h" 9 #include "byte.h"
10 #include "alloc.h"
10 #include "cdb.h" 11 #include "cdb.h"
11 12
12 13
13 static Cdb c; 14 /*static Cdb c;*/
14 15
15 Cdb* cdb_new(void) 16 Cdb* cdb_new(void)
16 { 17 {
17 return &c; 18 return (Cdb *)(alloc(sizeof(Cdb)));
18 } 19 }
19 20
20 void cdb_free(Cdb *c) 21 void cdb_clear(Cdb *c)
21 { 22 {
22 if (c->map) { 23 if (c->map) {
23 munmap(c->map,c->size); 24 munmap(c->map,c->size);
24 c->map = 0; 25 c->map = 0;
25 } 26 }
26 } 27 }
27 28
28 uint32 cdb_len() { 29 /* uint32 cdb_len() { */
29 return c.dlen; 30 /* return c.dlen; */
30 } 31 /* } */
31 32
32 uint32 cdb_pos() { 33 /* uint32 cdb_pos() { */
33 return c.dpos; 34 /* return c.dpos; */
34 } 35 /* } */
35 36
36 char *cdb_mmap() { 37 /* char *cdb_mmap() { */
37 return c.map; 38 /* return c.map; */
38 } 39 /* } */
39 40
40 uint32 cdb_msize() { 41 /* uint32 cdb_msize() { */
41 return c.size; 42 /* return c.size; */
42 } 43 /* } */
43 44
44 void cdb_findstart(Cdb *c) 45 void cdb_findstart(Cdb *c)
45 { 46 {
46 c->loop = 0; 47 c->loop = 0;
47 } 48 }
49 void cdb_init(Cdb *c,int fd) 50 void cdb_init(Cdb *c,int fd)
50 { 51 {
51 struct stat st; 52 struct stat st;
52 char *x; 53 char *x;
53 54
54 cdb_free(c); 55 cdb_clear(c);
55 cdb_findstart(c); 56 cdb_findstart(c);
56 c->fd = fd; 57 c->fd = fd;
57 58
58 if (fstat(fd,&st) == 0) 59 if (fstat(fd,&st) == 0)
59 if (st.st_size <= 0xffffffff) { 60 if (st.st_size <= 0xffffffff) {