File: mcache_api.h

package info (click to toggle)
librnd 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,812 kB
  • sloc: ansic: 126,990; sh: 2,602; makefile: 2,145; awk: 7
file content (49 lines) | stat: -rw-r--r-- 1,520 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef LIBUALLOC_MCACHE_API_H
#define LIBUALLOC_MCACHE_API_H

#include <libualloc/libualloc.h>

/*
	Allocator: mcache - memory allocation cache

	Remember all allocations per context in two doubly linked lists: one
	for currently in use, one for currently free (cache). Serve new allocations
	from the cache. Same as acache, but is more expensive and has clean().

	Allocation size:      fixed
	Standard calls:       alloc, free, clean
	Per allocation cost:  2 pointers
	Per page cost:        n/a (not paged)
*/

typedef struct uall_mcache_elem_s uall_mcache_elem_t;
typedef struct uall_mcache_page_s uall_mcache_page_t;

typedef struct {
	/* configuration */
	uall_sysalloc_t *sys;
	long elem_size;

	void *user_data;

	/* internal states - init all bytes to 0 */
	uall_mcache_elem_t *free_elems; /* singly linked list cache */
	uall_mcache_elem_t *used_elems; /* singly linked list cache */
} uall_mcache_t;


/* Return a new allocation of ctx->elem_size */
UALL_INLINE void *uall_mcache_alloc(uall_mcache_t *ctx);

/* Free a previous allocation (really just cache it for later reuse) */
UALL_INLINE void uall_mcache_free(uall_mcache_t *ctx, void *ptr);

/* Free all allocated and cached blocks; after the call ctx is ready to
   accept new allocations; cheaper than calling uall_mcache_free()
   multiple times */
UALL_INLINE void uall_mcache_clean(uall_mcache_t *ctx);

/* Free all unused (cached free) elements (but keep elements that are in use) */
UALL_INLINE void uall_mcache_flush(uall_mcache_t *ctx);

#endif