include the capn_segment as a header when allocating

This commit is contained in:
James McKaskill 2013-05-11 22:44:38 -04:00
parent 65c23b9218
commit 85b7a99429
2 changed files with 26 additions and 25 deletions

View file

@ -4,17 +4,22 @@
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
struct check_segment_alignment {
char foo : (sizeof(struct capn_segment)&7) ? -1 : 1;
};
static struct capn_segment *create(void *u, uint32_t id, int sz) { static struct capn_segment *create(void *u, uint32_t id, int sz) {
struct capn_segment *s; struct capn_segment *s;
sz += sizeof(*s);
if (sz < 4096) { if (sz < 4096) {
sz = 4096; sz = 4096;
} else { } else {
sz = (sz + 4095) & ~4095; sz = (sz + 4095) & ~4095;
} }
s = (struct capn_segment*) calloc(1, sizeof(*s)); s = (struct capn_segment*) calloc(1, sz);
s->data = (char*) calloc(1, sz); s->data = (char*) (s+1);
s->cap = sz; s->cap = sz - sizeof(*s);
s->user = (void*)(uintptr_t) 1; s->user = s;
return s; return s;
} }
@ -27,21 +32,21 @@ void capn_free(struct capn *c) {
struct capn_segment *s = c->seglist; struct capn_segment *s = c->seglist;
while (s != NULL) { while (s != NULL) {
struct capn_segment *n = s->next; struct capn_segment *n = s->next;
if (s->user) { free(s->user);
free(s->data);
free(s);
}
s = n; s = n;
} }
s = c->copylist; capn_reset_copy(c);
}
void capn_reset_copy(struct capn *c) {
struct capn_segment *s = c->copylist;
while (s != NULL) { while (s != NULL) {
struct capn_segment *n = s->next; struct capn_segment *n = s->next;
if (s->user) { free(s->user);
free(s->data);
free(s);
}
s = n; s = n;
} }
c->copy = NULL;
c->copylist = NULL;
} }
#define ZBUF_SZ 4096 #define ZBUF_SZ 4096
@ -96,10 +101,6 @@ static int init_fp(struct capn *c, FILE *f, struct capn_stream *z, int packed) {
goto err; goto err;
segnum++; segnum++;
s = (struct capn_segment*) calloc(segnum, sizeof(*s));
if (!s)
goto err;
if (read_fp(hdr, 8 * (segnum/2) + 4, f, z, zbuf, packed)) if (read_fp(hdr, 8 * (segnum/2) + 4, f, z, zbuf, packed))
goto err; goto err;
@ -107,32 +108,31 @@ static int init_fp(struct capn *c, FILE *f, struct capn_stream *z, int packed) {
uint32_t n = capn_flip32(hdr[i]); uint32_t n = capn_flip32(hdr[i]);
if (n > INT_MAX/8 || n > UINT32_MAX/8 || UINT32_MAX - total < n*8) if (n > INT_MAX/8 || n > UINT32_MAX/8 || UINT32_MAX - total < n*8)
goto err; goto err;
s[i].cap = s[i].len = n * 8; hdr[i] = n*8;
total += s[i].len; total += hdr[i];
} }
data = (char*) calloc(1, total); s = (struct capn_segment*) calloc(1, total + (sizeof(*s) * segnum));
if (!data) if (!s)
goto err; goto err;
data = (char*) (s+segnum);
if (read_fp(data, total, f, z, zbuf, packed)) if (read_fp(data, total, f, z, zbuf, packed))
goto err; goto err;
for (i = 0; i < segnum; i++) { for (i = 0; i < segnum; i++) {
s[i].len = s[i].cap = hdr[i];
s[i].data = data; s[i].data = data;
data += s[i].len; data += s[i].len;
capn_append_segment(c, &s[i]); capn_append_segment(c, &s[i]);
} }
/* mark the first segment so capn_free will free the data and s[segnum-1].user = s;
* segment arrays */
s[0].user = (void*)(uintptr_t) 1;
return 0; return 0;
err: err:
memset(c, 0, sizeof(*c)); memset(c, 0, sizeof(*c));
free(data);
free(s); free(s);
return -1; return -1;
} }

1
capn.h
View file

@ -218,6 +218,7 @@ int capn_init_fp(struct capn *c, FILE *f, int packed);
int capn_init_mem(struct capn *c, const uint8_t *p, size_t sz, int packed); int capn_init_mem(struct capn *c, const uint8_t *p, size_t sz, int packed);
void capn_free(struct capn *c); void capn_free(struct capn *c);
void capn_reset_copy(struct capn *c);
/* capn_stream encapsulates the needed fields for capn_(deflate|inflate) in a /* capn_stream encapsulates the needed fields for capn_(deflate|inflate) in a
* similar manner to z_stream from zlib * similar manner to z_stream from zlib