GNU libmicrohttpd 1.0.2
Loading...
Searching...
No Matches
memorypool.c File Reference

memory pool More...

#include "memorypool.h"
#include <string.h>
#include <stdint.h>
#include "mhd_assert.h"
#include "mhd_limits.h"
Include dependency graph for memorypool.c:

Go to the source code of this file.

Macros

#define _MHD_FALLBACK_PAGE_SIZE   (4096)
#define MHD_DEF_PAGE_SIZE_   _MHD_FALLBACK_PAGE_SIZE
#define MAP_FAILED   ((void*) -1)
#define ALIGN_SIZE   (2 * sizeof(void*))
#define ROUND_TO_ALIGN(n)
#define _MHD_NOSANITIZE_PTRS
#define _MHD_RED_ZONE_SIZE   (0)
#define ROUND_TO_ALIGN_PLUS_RED_ZONE(n)
#define _MHD_POISON_MEMORY(pointer, size)
#define _MHD_UNPOISON_MEMORY(pointer, size)
#define mp_ptr_le_(p1, p2)
#define mp_ptr_diff_(p1, p2)

Functions

void MHD_init_mem_pools_ (void)
struct MemoryPool * MHD_pool_create (size_t max)
void MHD_pool_destroy (struct MemoryPool *pool)
size_t MHD_pool_get_free (struct MemoryPool *pool)
void * MHD_pool_allocate (struct MemoryPool *pool, size_t size, bool from_end)
bool MHD_pool_is_resizable_inplace (struct MemoryPool *pool, void *block, size_t block_size)
void * MHD_pool_try_alloc (struct MemoryPool *pool, size_t size, size_t *required_bytes)
void * MHD_pool_reallocate (struct MemoryPool *pool, void *old, size_t old_size, size_t new_size)
void MHD_pool_deallocate (struct MemoryPool *pool, void *block, size_t block_size)
void * MHD_pool_reset (struct MemoryPool *pool, void *keep, size_t copy_bytes, size_t new_size)

Variables

static size_t MHD_sys_page_size_

Detailed Description

memory pool

Author
Christian Grothoff
Karlson2k (Evgeny Grin)

Definition in file memorypool.c.

Macro Definition Documentation

◆ _MHD_FALLBACK_PAGE_SIZE

#define _MHD_FALLBACK_PAGE_SIZE   (4096)

Fallback value of page size

Definition at line 62 of file memorypool.c.

◆ _MHD_NOSANITIZE_PTRS

#define _MHD_NOSANITIZE_PTRS

Definition at line 100 of file memorypool.c.

◆ _MHD_POISON_MEMORY

#define _MHD_POISON_MEMORY ( pointer,
size )
Value:
(void)0

Definition at line 103 of file memorypool.c.

Referenced by MHD_pool_create(), MHD_pool_deallocate(), MHD_pool_reallocate(), and MHD_pool_reset().

◆ _MHD_RED_ZONE_SIZE

#define _MHD_RED_ZONE_SIZE   (0)

Definition at line 101 of file memorypool.c.

Referenced by MHD_pool_deallocate(), MHD_pool_get_free(), and MHD_pool_reallocate().

◆ _MHD_UNPOISON_MEMORY

#define _MHD_UNPOISON_MEMORY ( pointer,
size )
Value:
(void)0

Definition at line 104 of file memorypool.c.

Referenced by MHD_pool_allocate(), MHD_pool_destroy(), MHD_pool_reallocate(), MHD_pool_reset(), and MHD_pool_try_alloc().

◆ ALIGN_SIZE

#define ALIGN_SIZE   (2 * sizeof(void*))

Align to 2x word size (as GNU libc does).

Definition at line 90 of file memorypool.c.

Referenced by MHD_init_mem_pools_(), and MHD_pool_create().

◆ MAP_FAILED

#define MAP_FAILED   ((void*) -1)

Definition at line 84 of file memorypool.c.

Referenced by MHD_pool_create().

◆ MHD_DEF_PAGE_SIZE_

#define MHD_DEF_PAGE_SIZE_   _MHD_FALLBACK_PAGE_SIZE

Definition at line 69 of file memorypool.c.

Referenced by MHD_init_mem_pools_().

◆ mp_ptr_diff_

#define mp_ptr_diff_ ( p1,
p2 )
Value:
((size_t)(((const uint8_t*)(p1)) - ((const uint8_t*)(p2))))

The difference in bytes between positions of the first and the second pointers

Definition at line 114 of file memorypool.c.

Referenced by MHD_pool_deallocate(), MHD_pool_is_resizable_inplace(), MHD_pool_reallocate(), and MHD_pool_reset().

◆ mp_ptr_le_

#define mp_ptr_le_ ( p1,
p2 )
Value:
(((const uint8_t*)(p1)) <= ((const uint8_t*)(p2)))

Boolean 'true' if the first pointer is less or equal the second pointer

Definition at line 108 of file memorypool.c.

Referenced by MHD_pool_deallocate(), MHD_pool_is_resizable_inplace(), MHD_pool_reallocate(), and MHD_pool_reset().

◆ ROUND_TO_ALIGN

#define ROUND_TO_ALIGN ( n)
Value:
(((n) + (ALIGN_SIZE - 1)) \
#define ALIGN_SIZE
Definition memorypool.c:90

Round up 'n' to a multiple of ALIGN_SIZE.

Definition at line 95 of file memorypool.c.

Referenced by MHD_pool_allocate(), MHD_pool_create(), MHD_pool_deallocate(), MHD_pool_destroy(), MHD_pool_get_free(), MHD_pool_reallocate(), and MHD_pool_try_alloc().

◆ ROUND_TO_ALIGN_PLUS_RED_ZONE

#define ROUND_TO_ALIGN_PLUS_RED_ZONE ( n)

Function Documentation

◆ MHD_init_mem_pools_()

void MHD_init_mem_pools_ ( void )

Initialise values for memory pools

Definition at line 209 of file memorypool.c.

References ALIGN_SIZE, mhd_assert, MHD_DEF_PAGE_SIZE_, and MHD_sys_page_size_.

Referenced by MHD_init().

Here is the caller graph for this function:

◆ MHD_pool_allocate()

void * MHD_pool_allocate ( struct MemoryPool * pool,
size_t size,
bool from_end )

Allocate size bytes from the pool.

Parameters
poolmemory pool to use for the operation
sizenumber of bytes to allocate
from_endallocate from end of pool (set to 'true'); use this for small, persistent allocations that will never be reallocated
Returns
NULL if the pool cannot support size more bytes

Definition at line 399 of file memorypool.c.

References _MHD_UNPOISON_MEMORY, mhd_assert, NULL, ROUND_TO_ALIGN, and ROUND_TO_ALIGN_PLUS_RED_ZONE.

Referenced by MHD_connection_alloc_memory_(), and MHD_connection_set_initial_state_().

Here is the caller graph for this function:

◆ MHD_pool_create()

struct MemoryPool * MHD_pool_create ( size_t max)

Create a memory pool.

Parameters
maxmaximum size of the pool
Returns
NULL on error

Definition at line 270 of file memorypool.c.

References _MHD_POISON_MEMORY, ALIGN_SIZE, MAP_FAILED, mhd_assert, MHD_sys_page_size_, NULL, and ROUND_TO_ALIGN.

Referenced by new_connection_process_().

Here is the caller graph for this function:

◆ MHD_pool_deallocate()

void MHD_pool_deallocate ( struct MemoryPool * pool,
void * block,
size_t block_size )

Deallocate a block of memory obtained from the pool.

If the given block is not the most recently (re)allocated block, the memory of the this block allocation may be not released until the pool is destroyed or reset.

Parameters
poolmemory pool to use for the operation
blockthe allocated block, the NULL is tolerated
block_sizethe size of the allocated block

Definition at line 625 of file memorypool.c.

References _MHD_POISON_MEMORY, _MHD_RED_ZONE_SIZE, mhd_assert, mp_ptr_diff_, mp_ptr_le_, NULL, ROUND_TO_ALIGN, and ROUND_TO_ALIGN_PLUS_RED_ZONE.

Referenced by connection_shrink_read_buffer(), and transmit_error_response_len().

Here is the caller graph for this function:

◆ MHD_pool_destroy()

void MHD_pool_destroy ( struct MemoryPool * pool)

Destroy a memory pool.

Parameters
poolmemory pool to destroy

Definition at line 341 of file memorypool.c.

References _MHD_UNPOISON_MEMORY, mhd_assert, NULL, and ROUND_TO_ALIGN.

Referenced by MHD_cleanup_connections(), MHD_connection_close_(), and new_connection_process_().

Here is the caller graph for this function:

◆ MHD_pool_get_free()

size_t MHD_pool_get_free ( struct MemoryPool * pool)

Check how much memory is left in the pool

Parameters
poolpool to check
Returns
number of bytes still available in pool

Definition at line 374 of file memorypool.c.

References _MHD_RED_ZONE_SIZE, mhd_assert, and ROUND_TO_ALIGN.

Referenced by connection_maximize_write_buffer(), try_grow_read_buffer(), and try_ready_chunked_body().

Here is the caller graph for this function:

◆ MHD_pool_is_resizable_inplace()

bool MHD_pool_is_resizable_inplace ( struct MemoryPool * pool,
void * block,
size_t block_size )

Checks whether allocated block is re-sizable in-place. If block is not re-sizable in-place, it still could be shrunk, but freed memory will not be re-used until reset of the pool.

Parameters
poolthe memory pool to use
blockthe pointer to the allocated block to check
block_sizethe size of the allocated block
Returns
true if block can be resized in-place in the optimal way, false otherwise

Definition at line 440 of file memorypool.c.

References mhd_assert, mp_ptr_diff_, mp_ptr_le_, NULL, and ROUND_TO_ALIGN_PLUS_RED_ZONE.

Referenced by connection_maximize_write_buffer(), connection_shrink_read_buffer(), MHD_connection_alloc_memory_(), try_grow_read_buffer(), and try_ready_chunked_body().

Here is the caller graph for this function:

◆ MHD_pool_reallocate()

void * MHD_pool_reallocate ( struct MemoryPool * pool,
void * old,
size_t old_size,
size_t new_size )

Reallocate a block of memory obtained from the pool. This is particularly efficient when growing or shrinking the block that was last (re)allocated. If the given block is not the most recently (re)allocated block, the memory of the previous allocation may be not released until the pool is destroyed or reset.

Parameters
poolmemory pool to use for the operation
oldthe existing block
old_sizethe size of the existing block
new_sizethe new size of the block
Returns
new address of the block, or NULL if the pool cannot support new_size bytes (old continues to be valid for old_size)

Definition at line 533 of file memorypool.c.

References _MHD_POISON_MEMORY, _MHD_RED_ZONE_SIZE, _MHD_UNPOISON_MEMORY, mhd_assert, mp_ptr_diff_, mp_ptr_le_, NULL, ROUND_TO_ALIGN, and ROUND_TO_ALIGN_PLUS_RED_ZONE.

Referenced by connection_maximize_write_buffer(), connection_shrink_read_buffer(), MHD_connection_alloc_memory_(), MHD_connection_handle_read(), try_grow_read_buffer(), and try_ready_chunked_body().

Here is the caller graph for this function:

◆ MHD_pool_reset()

void * MHD_pool_reset ( struct MemoryPool * pool,
void * keep,
size_t copy_bytes,
size_t new_size )

Clear all entries from the memory pool except for keep of the given copy_bytes. The pointer returned should be a buffer of new_size where the first copy_bytes are from keep.

Parameters
poolmemory pool to use for the operation
keeppointer to the entry to keep (maybe NULL)
copy_byteshow many bytes need to be kept at this address
new_sizehow many bytes should the allocation we return have? (should be larger or equal to copy_bytes)
Returns
addr new address of keep (if it had to change)

Size of area to zero-out

Definition at line 729 of file memorypool.c.

References _MHD_POISON_MEMORY, _MHD_UNPOISON_MEMORY, mhd_assert, MHD_sys_page_size_, mp_ptr_diff_, mp_ptr_le_, NULL, and ROUND_TO_ALIGN_PLUS_RED_ZONE.

Referenced by connection_reset(), and transmit_error_response_len().

Here is the caller graph for this function:

◆ MHD_pool_try_alloc()

void * MHD_pool_try_alloc ( struct MemoryPool * pool,
size_t size,
size_t * required_bytes )

Try to allocate size bytes memory area from the pool.

If allocation fails, required_bytes is updated with size required to be freed in the pool from rellocatable area to allocate requested number of bytes. Allocated memory area is always not rellocatable ("from end").

Parameters
poolmemory pool to use for the operation
sizethe size of memory in bytes to allocate
[out]required_bytesthe pointer to variable to be updated with the size of the required additional free memory area, set to 0 if function succeeds. Cannot be NULL.
Returns
the pointer to allocated memory area if succeed, NULL if the pool doesn't have enough space, required_bytes is updated with amount of space needed to be freed in rellocatable area or set to SIZE_MAX if requested size is too large for the pool.

Definition at line 481 of file memorypool.c.

References _MHD_UNPOISON_MEMORY, mhd_assert, NULL, ROUND_TO_ALIGN, ROUND_TO_ALIGN_PLUS_RED_ZONE, and SIZE_MAX.

Referenced by MHD_connection_alloc_memory_().

Here is the caller graph for this function:

Variable Documentation

◆ MHD_sys_page_size_

size_t MHD_sys_page_size_
static
Initial value:
= (size_t)
#define _MHD_FALLBACK_PAGE_SIZE
Definition memorypool.c:62

Size of memory page

Definition at line 196 of file memorypool.c.

Referenced by MHD_init_mem_pools_(), MHD_pool_create(), and MHD_pool_reset().