return offset;
}
+static void loc_stringpool_free(struct loc_stringpool* pool) {
+ DEBUG(pool->ctx, "Releasing string pool %p\n", pool);
+ int r;
+
+ switch (pool->mode) {
+ case STRINGPOOL_DEFAULT:
+ if (pool->data)
+ free(pool->data);
+ break;
+
+ case STRINGPOOL_MMAP:
+ if (pool->data) {
+ r = munmap(pool->data, pool->length);
+ if (r)
+ ERROR(pool->ctx, "Could not unmap data at %p: %s\n",
+ pool->data, strerror(errno));
+ }
+ break;
+ }
+
+ loc_unref(pool->ctx);
+ free(pool);
+}
+
LOC_EXPORT int loc_stringpool_new(struct loc_ctx* ctx, struct loc_stringpool** pool) {
struct loc_stringpool* p = calloc(1, sizeof(*p));
if (!p)
pool->length = length;
if (pool->data == MAP_FAILED)
- return -errno;
+ return 1;
return 0;
}
LOC_EXPORT int loc_stringpool_open(struct loc_ctx* ctx, struct loc_stringpool** pool,
FILE* f, size_t length, off_t offset) {
- int r = loc_stringpool_new(ctx, pool);
+ struct loc_stringpool* p = NULL;
+
+ // Allocate a new stringpool
+ int r = loc_stringpool_new(ctx, &p);
if (r)
return r;
- struct loc_stringpool* p = *pool;
-
// Change mode to mmap
p->mode = STRINGPOOL_MMAP;
// Map data into memory
if (length > 0) {
r = loc_stringpool_mmap(p, f, length, offset);
- if (r)
+ if (r) {
+ loc_stringpool_free(p);
return r;
+ }
}
+ *pool = p;
return 0;
}
return pool;
}
-static void loc_stringpool_free(struct loc_stringpool* pool) {
- DEBUG(pool->ctx, "Releasing string pool %p\n", pool);
- int r;
-
- switch (pool->mode) {
- case STRINGPOOL_DEFAULT:
- if (pool->data)
- free(pool->data);
- break;
-
- case STRINGPOOL_MMAP:
- if (pool->data) {
- r = munmap(pool->data, pool->length);
- if (r)
- ERROR(pool->ctx, "Could not unmap data at %p: %s\n",
- pool->data, strerror(errno));
- }
- break;
- }
-
- loc_unref(pool->ctx);
- free(pool);
-}
-
LOC_EXPORT struct loc_stringpool* loc_stringpool_unref(struct loc_stringpool* pool) {
if (--pool->refcount > 0)
return NULL;