* @chunk_size must be in the interval `[1, UINT_MAX / 2]`.
* It will allocate memory by this large chunks and take
* memory to satisfy requests from them.
+ *
+ * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
**/
void mp_init(struct mempool *pool, uns chunk_size);
* See @mp_init() for @chunk_size limitations.
*
* The new mempool structure is allocated on the new mempool.
+ *
+ * Memory pools can be treated as <<trans:respools,resources>>, see <<trans:res_mempool()>>.
**/
struct mempool *mp_new(uns chunk_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);
{
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;
}
{
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);
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);
**/
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];
}
/**
**/
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));
}
/**
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;
}
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];
}
/**
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;
}
{
mp_open_fast(pool, ptr);
ptr = mp_grow(pool, size);
- mp_end(pool, ptr + size);
+ mp_end(pool, (byte *)ptr + size);
return ptr;
}
/**
* Save the current state of a memory pool.
- * Do not call this function with an opened growing buffer.
+ * Do not call this function with an opened growing buffer.
**/
static inline void mp_save(struct mempool *pool, struct mempool_state *state)
{
* -----------------
***/
-char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; /** Makes a copy of a string on a mempool. **/
+char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; /** Makes a copy of a string on a mempool. Returns NULL for NULL string. **/
void *mp_memdup(struct mempool *, const void *, uns) LIKE_MALLOC; /** Makes a copy of a memory block on a mempool. **/
/**
* Concatenates all passed strings. The last parameter must be NULL.
* tells how many there is of them.
**/
char *mp_strjoin(struct mempool *p, char **a, uns n, uns sep) LIKE_MALLOC;
+/**
+ * Convert memory block to a string. Makes a copy of the given memory block
+ * in the mempool @p, adding an extra terminating zero byte at the end.
+ **/
+char *mp_str_from_mem(struct mempool *p, const void *mem, uns len) LIKE_MALLOC;
/***