1570af302Sopenharmony_ci#ifndef MALLOC_IMPL_H
2570af302Sopenharmony_ci#define MALLOC_IMPL_H
3570af302Sopenharmony_ci
4570af302Sopenharmony_ci#include <sys/mman.h>
5570af302Sopenharmony_ci#ifdef __LITEOS_DEBUG__
6570af302Sopenharmony_ci#include <stdbool.h>
7570af302Sopenharmony_ci
8570af302Sopenharmony_ci#define BACKTRACE_DEPTH_MAX      5          /* The max depth of backtrace */
9570af302Sopenharmony_ci#define BACKTRACE_START_OFFSET   0          /* The start offset of backtrace */
10570af302Sopenharmony_ci#define SECONDARY_CALL_OFFSET    2          /* The backtrace offset for secondary call backtrace() */
11570af302Sopenharmony_ci#define BACKTRACE_OFFSET         (BACKTRACE_START_OFFSET + SECONDARY_CALL_OFFSET)
12570af302Sopenharmony_ci#define PREFIX_PLACE_HOLDER      10         /* Reserve positions for file name prefix "pid()_" */
13570af302Sopenharmony_ci#define PTHREAD_NUM_MAX          128        /* Same as number of task of kernel */
14570af302Sopenharmony_ci#define NODE_MAGIC               0xFCFCFCFC /* Magic number for node of chunk */
15570af302Sopenharmony_ci#define FREE_MAGIC               0xFE       /* Magic number for filling freed heap memory not recycled to heap pool */
16570af302Sopenharmony_ci#define RECYCLE_MAX              128        /* The queue size for free() to recycle */
17570af302Sopenharmony_ci#define RECYCLE_SIZE_MAX         0x300000   /* The max sum size of freed chunk for recycle list */
18570af302Sopenharmony_ci#define ITEM_BUFFER_SIZE         256        /* The buffer max size for one item of memory debug info */
19570af302Sopenharmony_ci#define CHECK_POINT_TRACE_MAX    2          /* The trace max for check point */
20570af302Sopenharmony_ci
21570af302Sopenharmony_cihidden void *__expand_heap(size_t *);
22570af302Sopenharmony_ci
23570af302Sopenharmony_cihidden void __malloc_donate(char *, char *);
24570af302Sopenharmony_ci
25570af302Sopenharmony_cihidden void *__memalign(size_t, size_t);
26570af302Sopenharmony_ci#else
27570af302Sopenharmony_ci#include "dynlink.h"
28570af302Sopenharmony_ci#endif
29570af302Sopenharmony_ci
30570af302Sopenharmony_cistruct chunk {
31570af302Sopenharmony_ci#ifdef __LITEOS_DEBUG__
32570af302Sopenharmony_ci	unsigned int checksum;
33570af302Sopenharmony_ci#endif
34570af302Sopenharmony_ci	size_t psize, csize;
35570af302Sopenharmony_ci	struct chunk *next, *prev;
36570af302Sopenharmony_ci};
37570af302Sopenharmony_ci
38570af302Sopenharmony_cistruct bin {
39570af302Sopenharmony_ci#ifdef __LITEOS_DEBUG__
40570af302Sopenharmony_ci	unsigned int checksum;
41570af302Sopenharmony_ci#endif
42570af302Sopenharmony_ci	volatile int lock[2];
43570af302Sopenharmony_ci	struct chunk *head;
44570af302Sopenharmony_ci	struct chunk *tail;
45570af302Sopenharmony_ci};
46570af302Sopenharmony_ci
47570af302Sopenharmony_ci#ifdef __LITEOS_DEBUG__
48570af302Sopenharmony_cistruct heap_block {
49570af302Sopenharmony_ci	struct heap_block *next;
50570af302Sopenharmony_ci	struct heap_block *prev;
51570af302Sopenharmony_ci};
52570af302Sopenharmony_ci
53570af302Sopenharmony_cistruct list {
54570af302Sopenharmony_ci	struct list *prev;
55570af302Sopenharmony_ci	struct list *next;
56570af302Sopenharmony_ci};
57570af302Sopenharmony_ci
58570af302Sopenharmony_cistruct node {
59570af302Sopenharmony_ci	short tid, pid;
60570af302Sopenharmony_ci	void *ptr;
61570af302Sopenharmony_ci	size_t size;
62570af302Sopenharmony_ci	void *lr[BACKTRACE_DEPTH_MAX];
63570af302Sopenharmony_ci	struct list list;
64570af302Sopenharmony_ci};
65570af302Sopenharmony_ci
66570af302Sopenharmony_cistruct stat_bin {
67570af302Sopenharmony_ci	volatile int lock[2];
68570af302Sopenharmony_ci	struct list head;
69570af302Sopenharmony_ci	size_t t_total_size;
70570af302Sopenharmony_ci};
71570af302Sopenharmony_ci
72570af302Sopenharmony_ci#define ROUNDUP(a, b) (((a) + ((b) - 1)) & ~((b) - 1))
73570af302Sopenharmony_ci#define SIZE_ALIGN ROUNDUP(sizeof(struct chunk), 0x10)
74570af302Sopenharmony_ci#define SIZE_MASK (-SIZE_ALIGN)
75570af302Sopenharmony_ci#define OVERHEAD (sizeof(struct chunk))
76570af302Sopenharmony_ci#define BLOCK_HEAD (sizeof(struct heap_block) + OVERHEAD)
77570af302Sopenharmony_ci#define CHUNK_BLOCK_OFFSET (sizeof(struct heap_block))
78570af302Sopenharmony_ci#define CHUNK_TO_BLOCK(c) (struct heap_block *)((char *)(c) - CHUNK_BLOCK_OFFSET)
79570af302Sopenharmony_ci#define BLOCK_TO_CHUNK(p) (struct chunk *)((char *)(p) + CHUNK_BLOCK_OFFSET)
80570af302Sopenharmony_ci#define MMAP_THRESHOLD (0x1c00*(4*sizeof(size_t)))
81570af302Sopenharmony_ci#define DONTCARE SIZE_ALIGN
82570af302Sopenharmony_ci#else
83570af302Sopenharmony_ci#define SIZE_ALIGN (4*sizeof(size_t))
84570af302Sopenharmony_ci#define SIZE_MASK (-SIZE_ALIGN)
85570af302Sopenharmony_ci#define OVERHEAD (2*sizeof(size_t))
86570af302Sopenharmony_ci#define MMAP_THRESHOLD (0x1c00*SIZE_ALIGN)
87570af302Sopenharmony_ci#define DONTCARE 16
88570af302Sopenharmony_ci#endif
89570af302Sopenharmony_ci#define RECLAIM 163840
90570af302Sopenharmony_ci
91570af302Sopenharmony_ci#define CHUNK_SIZE(c) ((c)->csize & -2)
92570af302Sopenharmony_ci#define CHUNK_PSIZE(c) ((c)->psize & -2)
93570af302Sopenharmony_ci#define PREV_CHUNK(c) ((struct chunk *)((char *)(c) - CHUNK_PSIZE(c)))
94570af302Sopenharmony_ci#define NEXT_CHUNK(c) ((struct chunk *)((char *)(c) + CHUNK_SIZE(c)))
95570af302Sopenharmony_ci#define MEM_TO_CHUNK(p) (struct chunk *)((char *)(p) - OVERHEAD)
96570af302Sopenharmony_ci#define CHUNK_TO_MEM(c) (void *)((char *)(c) + OVERHEAD)
97570af302Sopenharmony_ci#ifdef __LITEOS_DEBUG__
98570af302Sopenharmony_ci#define BIN_TO_CHUNK(i) (&mal.bins[i].checksum)
99570af302Sopenharmony_ci#else
100570af302Sopenharmony_ci#define BIN_TO_CHUNK(i) (MEM_TO_CHUNK(&mal.bins[i].head))
101570af302Sopenharmony_ci#endif
102570af302Sopenharmony_ci
103570af302Sopenharmony_ci#define C_INUSE  ((size_t)1)
104570af302Sopenharmony_ci
105570af302Sopenharmony_ci#define IS_MMAPPED(c) !((c)->csize & (C_INUSE))
106570af302Sopenharmony_ci
107570af302Sopenharmony_cihidden void __bin_chunk(struct chunk *);
108570af302Sopenharmony_ci
109570af302Sopenharmony_ci#ifdef __LITEOS_DEBUG__
110570af302Sopenharmony_cihidden extern int __malloc_replaced;
111570af302Sopenharmony_ci
112570af302Sopenharmony_cihidden extern bool g_enable_check;
113570af302Sopenharmony_cihidden extern int g_recycle_num;
114570af302Sopenharmony_cihidden extern size_t g_recycle_size;
115570af302Sopenharmony_cihidden extern int g_mem_lock[];
116570af302Sopenharmony_cihidden extern void insert_node(void *ptr, size_t size);
117570af302Sopenharmony_cihidden extern int delete_node(void *ptr);
118570af302Sopenharmony_cihidden extern void insert_block_list(struct chunk *ptr);
119570af302Sopenharmony_cihidden extern void insert_free_tail(struct chunk *self);
120570af302Sopenharmony_cihidden extern struct chunk *get_free_head(void);
121570af302Sopenharmony_cihidden extern void get_free_trace(void *ptr);
122570af302Sopenharmony_cihidden extern void clean_recycle_list(bool clean_all);
123570af302Sopenharmony_cihidden extern void check_chunk_integrity(struct chunk *cur);
124570af302Sopenharmony_cihidden extern void calculate_checksum(struct chunk *cur, struct chunk *next);
125570af302Sopenharmony_ci#endif
126570af302Sopenharmony_ci#endif
127