X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ucw%2Fmempool.h;h=7ac7f1667acef3a503a41c0661d2f73972402445;hb=b453b78c24779eae3a51f9b8e6a439a834d89f96;hp=7e4b662d7d4a216453929531040c8230a6137e4a;hpb=6e6c3447234fc657c209fbed2dea20b08aabedcb;p=libucw.git diff --git a/ucw/mempool.h b/ucw/mempool.h index 7e4b662d..7ac7f166 100644 --- a/ucw/mempool.h +++ b/ucw/mempool.h @@ -73,7 +73,7 @@ struct mempool *mp_new(uns chunk_size); void mp_delete(struct mempool *pool); /** - * Free all data on a memory pool, but leaves it working. + * Frees all data on a memory pool, but leaves it working. * It can keep some of the chunks allocated to serve * further allocation requests. Leaves the @pool alive, * even if it was created with @mp_new(). @@ -128,7 +128,7 @@ static inline void *mp_alloc_fast(struct mempool *pool, uns size) if (size <= avail) { pool->state.free[0] = avail - size; - return pool->state.last[0] - avail; + return (byte *)pool->state.last[0] - avail; } else return mp_alloc_internal(pool, size); @@ -141,7 +141,7 @@ static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size) { if (size <= pool->state.free[0]) { - void *ptr = pool->state.last[0] - pool->state.free[0]; + void *ptr = (byte *)pool->state.last[0] - pool->state.free[0]; pool->state.free[0] -= size; return ptr; } @@ -157,6 +157,8 @@ static inline void *mp_alloc_fast_noalign(struct mempool *pool, uns size) * You do not need to know, how a buffer will need to be large, * you can grow it incrementally to needed size. You can grow only * one buffer at a time on a given mempool. + * + * Similar functionality is provided by <> module. ***/ /* For internal use only, do not call directly */ @@ -196,7 +198,7 @@ static inline void *mp_start_fast(struct mempool *pool, uns size) { pool->idx = 0; pool->state.free[0] = avail; - return pool->state.last[0] - avail; + return (byte *)pool->state.last[0] - avail; } else return mp_start_internal(pool, size); @@ -210,7 +212,7 @@ static inline void *mp_start_fast_noalign(struct mempool *pool, uns size) if (size <= pool->state.free[0]) { pool->idx = 0; - return pool->state.last[0] - pool->state.free[0]; + return (byte *)pool->state.last[0] - pool->state.free[0]; } else return mp_start_internal(pool, size); @@ -221,7 +223,7 @@ static inline void *mp_start_fast_noalign(struct mempool *pool, uns size) **/ static inline void *mp_ptr(struct mempool *pool) { - return pool->state.last[pool->idx] - pool->state.free[pool->idx]; + return (byte *)pool->state.last[pool->idx] - pool->state.free[pool->idx]; } /** @@ -258,7 +260,7 @@ static inline void *mp_expand(struct mempool *pool) **/ static inline void *mp_spread(struct mempool *pool, void *p, uns size) { - return (((uns)(pool->state.last[pool->idx] - p) >= size) ? p : mp_spread_internal(pool, p, size)); + return (((uns)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size)); } /** @@ -269,7 +271,7 @@ static inline void *mp_spread(struct mempool *pool, void *p, uns size) static inline void *mp_end(struct mempool *pool, void *end) { void *p = mp_ptr(pool); - pool->state.free[pool->idx] = pool->state.last[pool->idx] - end; + pool->state.free[pool->idx] = (byte *)pool->state.last[pool->idx] - (byte *)end; return p; } @@ -279,7 +281,7 @@ static inline void *mp_end(struct mempool *pool, void *end) static inline uns mp_size(struct mempool *pool, void *ptr) { uns idx = mp_idx(pool, ptr); - return pool->state.last[idx] - ptr - pool->state.free[idx]; + return ((byte *)pool->state.last[idx] - (byte *)ptr) - pool->state.free[idx]; } /** @@ -295,7 +297,7 @@ uns mp_open(struct mempool *pool, void *ptr); static inline uns mp_open_fast(struct mempool *pool, void *ptr) { pool->idx = mp_idx(pool, ptr); - uns size = pool->state.last[pool->idx] - ptr - pool->state.free[pool->idx]; + uns size = ((byte *)pool->state.last[pool->idx] - (byte *)ptr) - pool->state.free[pool->idx]; pool->state.free[pool->idx] += size; return size; } @@ -319,7 +321,7 @@ static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uns size) { mp_open_fast(pool, ptr); ptr = mp_grow(pool, size); - mp_end(pool, ptr + size); + mp_end(pool, (byte *)ptr + size); return ptr; } @@ -355,6 +357,20 @@ struct mempool_state *mp_push(struct mempool *pool); **/ void mp_restore(struct mempool *pool, struct mempool_state *state); +/** + * Inlined version of @mp_restore(). + **/ +static inline void mp_restore_fast(struct mempool *pool, struct mempool_state *state) +{ + if (pool->state.last[0] != state->last[0] || pool->state.last[1] != state->last[1]) + mp_restore(pool, state); + else + { + pool->state = *state; + pool->last_big = &pool->last_big; + } +} + /** * Restore the state saved by the last call to @mp_push(). * @mp_pop() and @mp_push() works as a stack so you can push more states safely. @@ -407,11 +423,11 @@ char *mp_printf(struct mempool *mp, const char *fmt, ...) FORMAT_CHECK(printf,2, **/ char *mp_vprintf(struct mempool *mp, const char *fmt, va_list args) LIKE_MALLOC; /** - * Like @mp_printf(), but it appends the data at the end of memory - * block pointed to by @ptr. The block is @mp_open()ed, so you have to + * Like @mp_printf(), but it appends the data at the end of string + * pointed to by @ptr. The string is @mp_open()ed, so you have to * provide something that can be. * - * Returns pointer to the beginning of the block (the pointer may have + * Returns pointer to the beginning of the string (the pointer may have * changed due to reallocation). **/ char *mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);