X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=lib%2Fmempool.h;h=c53423abc1130a1f8f4600c4535c3e543e4697cd;hb=1f05348cde42852d77da3457b96a33dff1600ca4;hp=c95b8ade2b5affc174e088acd50a03efd1f63c1c;hpb=33f4c9043db1681727e67f88b7dcf0bf8a0e61fc;p=libucw.git diff --git a/lib/mempool.h b/lib/mempool.h index c95b8ade..c53423ab 100644 --- a/lib/mempool.h +++ b/lib/mempool.h @@ -104,6 +104,7 @@ mp_alloc_fast_noalign(struct mempool *pool, uns size) /* For internal use only, do not call directly */ void *mp_start_internal(struct mempool *pool, uns size) LIKE_MALLOC; void *mp_grow_internal(struct mempool *pool, uns size); +void *mp_spread_internal(struct mempool *pool, void *p, uns size); static inline uns mp_idx(struct mempool *pool, void *ptr) @@ -186,12 +187,22 @@ mp_expand(struct mempool *pool) return mp_grow_internal(pool, mp_avail(pool) + 1); } +/* Ensure that there is at least bytes free after

, if not, reallocate and adjust

. */ +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)); +} + /* Close the growing buffer. The must point just behind the data, you want to keep - * allocated (so it can be in the interval [mp_ptr(pool), mp_ptr(pool) + mp_avail(pool)]). */ -static inline void + * allocated (so it can be in the interval [mp_ptr(pool), mp_ptr(pool) + mp_avail(pool)]). + * Returns a pointer to the beginning of the just closed block. */ +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; + return p; } /* Return size in bytes of the last allocated memory block (with mp_alloc*() or mp_end()). */ @@ -263,11 +274,11 @@ void mp_pop(struct mempool *pool); /*** mempool-str.c ***/ -char *mp_strdup(struct mempool *, char *) LIKE_MALLOC; -void *mp_memdup(struct mempool *, void *, uns) LIKE_MALLOC; +char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; +void *mp_memdup(struct mempool *, const void *, uns) LIKE_MALLOC; char *mp_multicat(struct mempool *, ...) LIKE_MALLOC SENTINEL_CHECK; static inline char * LIKE_MALLOC -mp_strcat(struct mempool *mp, char *x, char *y) +mp_strcat(struct mempool *mp, const char *x, const char *y) { return mp_multicat(mp, x, y, NULL); }