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;
}
* 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 <<growbuf:,growing buffes>> module.
***/
/* For internal use only, do not call directly */
{
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)
{
**/
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.
* -----------------
***/
-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.
**/
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);