X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb%2Fio.c;h=77f26adfe7df3eace208dee00f3f57aeaa1d28f0;hb=695e2ae319052ceb410ef00d0181aa607f1ceb6d;hp=59caa5dd04c37e13a63132231c7dd6986982b011;hpb=bcf7916c5d694858766869dfd570e525127154a6;p=ccan diff --git a/ccan/tdb/io.c b/ccan/tdb/io.c index 59caa5dd..77f26adf 100644 --- a/ccan/tdb/io.c +++ b/ccan/tdb/io.c @@ -125,9 +125,17 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off, /* Endian conversion: we only ever deal with 4 byte quantities */ void *tdb_convert(void *buf, uint32_t size) { - uint32_t i, *p = (uint32_t *)buf; - for (i = 0; i < size / 4; i++) - p[i] = TDB_BYTEREV(p[i]); + uint32_t i; + unsigned char *p = buf, tmp; + + for (i = 0; i < size; i += 4) { + tmp = p[i]; + p[i] = p[i+3]; + p[i+3] = tmp; + tmp = p[i+1]; + p[i+1] = p[i+2]; + p[i+2] = tmp; + } return buf; } @@ -194,7 +202,9 @@ int tdb_munmap(struct tdb_context *tdb) #if HAVE_MMAP if (tdb->map_ptr) { - int ret = munmap(tdb->map_ptr, tdb->map_size); + int ret; + + ret = munmap(tdb->map_ptr, tdb->map_size); if (ret != 0) return ret; } @@ -293,13 +303,39 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t ad return 0; } +/* You need 'size', this tells you how much you should expand by. */ +tdb_off_t tdb_expand_adjust(tdb_off_t map_size, tdb_off_t size, int page_size) +{ + tdb_off_t new_size, top_size; + + /* limit size in order to avoid using up huge amounts of memory for + * in memory tdbs if an oddball huge record creeps in */ + if (size > 100 * 1024) { + top_size = map_size + size * 2; + } else { + top_size = map_size + size * 100; + } + + /* always make room for at least top_size more records, and at + least 25% more space. if the DB is smaller than 100MiB, + otherwise grow it by 10% only. */ + if (map_size > 100 * 1024 * 1024) { + new_size = map_size * 1.10; + } else { + new_size = map_size * 1.25; + } + + /* Round the database up to a multiple of the page size */ + new_size = MAX(top_size, new_size); + return TDB_ALIGN(new_size, page_size) - map_size; +} /* expand the database at least size bytes by expanding the underlying file and doing the mmap again if necessary */ int tdb_expand(struct tdb_context *tdb, tdb_off_t size) { - struct list_struct rec; - tdb_off_t offset, new_size; + struct tdb_record rec; + tdb_off_t offset; if (tdb_lock(tdb, -1, F_WRLCK) == -1) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "lock failed in tdb_expand\n")); @@ -309,11 +345,7 @@ int tdb_expand(struct tdb_context *tdb, tdb_off_t size) /* must know about any previous expansions by another process */ tdb->methods->tdb_oob(tdb, tdb->map_size + 1, 1); - /* always make room for at least 100 more records, and at - least 25% more space. Round the database up to a multiple - of the page size */ - new_size = MAX(tdb->map_size + size*100, tdb->map_size * 1.25); - size = TDB_ALIGN(new_size, tdb->page_size) - tdb->map_size; + size = tdb_expand_adjust(tdb->map_size, size, tdb->page_size); if (!(tdb->flags & TDB_INTERNAL)) tdb_munmap(tdb); @@ -386,6 +418,7 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len unsigned char *buf; /* some systems don't like zero length malloc */ + if (!(buf = (unsigned char *)malloc(len ? len : 1))) { /* Ensure ecode is set for log fn. */ tdb->ecode = TDB_ERR_OOM; @@ -435,7 +468,7 @@ int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key, } /* read/write a record */ -int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec) +int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec) { if (tdb->methods->tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1) return -1; @@ -448,9 +481,9 @@ int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct * return tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0); } -int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec) +int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec) { - struct list_struct r = *rec; + struct tdb_record r = *rec; return tdb->methods->tdb_write(tdb, offset, CONVERT(r), sizeof(r)); } @@ -460,7 +493,6 @@ static const struct tdb_methods io_methods = { tdb_next_hash_chain, tdb_oob, tdb_expand_file, - tdb_brlock }; /*