My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sw_buddy.h
Go to the documentation of this file.
1 /*
2  * OpenVirtualization:
3  * For additional details and support contact developer@sierraware.com.
4  * Additional documentation can be found at www.openvirtualization.org
5  *
6  * Copyright (C) 2010-2014 SierraWare
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  */
23 /*
24  */
25 
30 #ifndef __SW_BUDDY_H_
31 #define __SW_BUDDY_H_
32 
33 #include <sw_list.h>
34 #include <sw_types.h>
35 
36 #ifndef CONFIG_BUDDY_HOUSE_KEEPING_PERCENT
37 #define CONFIG_BUDDY_HOUSE_KEEPING_PERCENT 25
38 #endif
39 
40 #ifndef CONFIG_BUDDY_MIN_BLOCK_SIZE_SHIFT
41 #define CONFIG_BUDDY_MIN_BLOCK_SIZE_SHIFT 2
42 #endif
43 
44 #ifndef CONFIG_BUDDY_MAX_BLOCK_SIZE_SHIFT
45 #define CONFIG_BUDDY_MAX_BLOCK_SIZE_SHIFT 12
46 #endif
47 
48 
49 #define HOUSE_KEEPING_PERCENT (CONFIG_BUDDY_HOUSE_KEEPING_PERCENT)
50 /* Minimum alloc of bus width */
51 #define MIN_BLOCK_SIZE (0x01UL << CONFIG_BUDDY_MIN_BLOCK_SIZE_SHIFT)
52 /* Maximum alloc of bus width */
53 #define MAX_BLOCK_SIZE (0x01UL << CONFIG_BUDDY_MAX_BLOCK_SIZE_SHIFT)
54 #define BINS_MAX_ORDER (CONFIG_BUDDY_MAX_BLOCK_SIZE_SHIFT - \
55  CONFIG_BUDDY_MIN_BLOCK_SIZE_SHIFT + 1)
56 
57 #define NUM_OF_HEAPS 8
58 #define HEAP_SIZE (1 << 16) /* 64 KB */
59 #define COMMON_HEAP_ID 0
60 
64 struct sw_free_area {
65  struct list head;
66  void *map;
67  unsigned int count;
68  unsigned int res0;
69 } __attribute__ ((packed));
70 
75  struct list head;
76  void *map;
77  unsigned int blk_sz;
78  unsigned int bin_num;
79  unsigned int count;
80  unsigned int res0;
81 } __attribute__ ((packed));
82 
86 struct sw_heap {
89  void *mem_start;
90  void *heap_start;
92  struct sw_free_area free_area[BINS_MAX_ORDER]; /* Bins holding free area. */
93  unsigned int hk_fn_count;
94  unsigned int hk_an_count;
95  unsigned int mem_size;
96  unsigned int heap_size;
97 } __attribute__ ((packed));
98 
108 int buddy_init(int index, void *heap_start, unsigned int heap_size);
109 
118 void *sw_buddy_malloc(int index, unsigned int size);
119 
127 void *buddy_zalloc(unsigned int size);
128 
135 void buddy_free(int index, void *ptr);
136 
140 void sw_buddy_print_state(void);
141 
145 void buddy_print_hk_state(int index);
146 
154 void *sw_malloc(u32 size);
155 
161 void sw_free(void *pointer);
162 
171 int sw_heap_init(int index, void *heap_start, unsigned int heap_size);
172 
180 unsigned int get_ptr_size(void* ptr);
181 
190 void *sw_malloc_private(int heap_id ,u32 size);
191 
198 void sw_free_private(int heap_id, void *pointer);
199 
207 int alloc_private_heap(int heap_id);
208 
216 int free_private_heap(int heap_id);
217 
218 void sw_malloc_init(void);
219 
220 void sw_dump_mallocs(void);
221 
222 void sw_malloc_free(void* ptr);
223 
224 #endif /* __SW_BUDDY_H_ */
unsigned int heap_size
Definition: sw_buddy.h:96
unsigned int blk_sz
Definition: sw_buddy.h:77
#define BINS_MAX_ORDER
Definition: sw_buddy.h:54
struct list head
Definition: sw_buddy.h:65
unsigned int bin_num
Definition: sw_buddy.h:78
int alloc_private_heap(int heap_id)
Allocates heap memory initially during task creation for a particular heap_id Checks for already allo...
Definition: sw_buddy.c:819
struct list head
Definition: sw_buddy.h:75
int buddy_init(int index, void *heap_start, unsigned int heap_size)
Buddy allocator initialization.
Definition: sw_buddy.c:119
int free_private_heap(int heap_id)
Frees the private heap already allocated corresponding to the heap_id.
Definition: sw_buddy.c:848
Definition: sw_buddy.h:86
Definition: sw_buddy.h:64
void * sw_malloc(u32 size)
Gets the task id and allocates memory in the heap memory corresponding to the task.
Definition: sw_buddy.c:690
unsigned int count
Definition: sw_buddy.h:79
void sw_malloc_init(void)
Initial Memory Allocation Heap also initialized.
Definition: sw_heap.c:55
int sw_heap_init(int index, void *heap_start, unsigned int heap_size)
Heap memory initialization.
Definition: sw_buddy.c:802
void * heap_start
Definition: sw_buddy.h:39
void buddy_print_hk_state(int index)
Prints the status of house keeping region of heap memory No. of free and allocated nodes in the heap...
Definition: sw_buddy.c:622
struct sw_alloced_area * hk_an_array
Definition: sw_buddy.h:88
unsigned int mem_size
Definition: sw_buddy.h:95
unsigned int get_ptr_size(void *ptr)
function to get the size of a pointer. This is needed during realloc
Definition: sw_buddy.c:881
struct sw_free_area * hk_fn_array
Definition: sw_buddy.h:87
void * mem_start
Definition: sw_buddy.h:89
Definition: sw_buddy.h:74
unsigned int res0
Definition: sw_buddy.h:68
unsigned int res0
Definition: sw_buddy.h:80
void * buddy_zalloc(unsigned int size)
Allocates memory at the requested heap index for size bytes and initalizes it to zero.
Definition: sw_buddy.c:523
void sw_free_private(int heap_id, void *pointer)
Frees the privately allocated memory pointing to the heap_id and the address pointer.
Definition: sw_buddy.c:735
struct sw_alloced_area current
Definition: sw_buddy.h:91
void * sw_buddy_malloc(int index, unsigned int size)
allocates memory at the requested heap index for size bytes
Definition: sw_buddy.c:452
void * sw_malloc_private(int heap_id, u32 size)
Allocates memory privately for size bytes at the memory related to heap_id passed.
Definition: sw_buddy.c:657
void sw_free(void *pointer)
Gets the task_id and frees the heap memory corresponding to the task_id.
Definition: sw_buddy.c:763
struct sw_free_area __attribute__((packed))
void * map
Definition: sw_buddy.h:76
void sw_malloc_free(void *ptr)
Frees the memory allocated by passing the address ptr.
Definition: sw_heap.c:37
void * map
Definition: sw_buddy.h:66
Definition: sw_list.h:56
void sw_dump_mallocs(void)
Prints the heap memory allocation related address and sizes.
Definition: sw_heap.c:46
unsigned int hk_fn_count
Definition: sw_buddy.h:93
void sw_buddy_print_state(void)
Prints the state of the heap allocations Whether heap is found for this task Heap size...
Definition: sw_buddy.c:578
unsigned int hk_an_count
Definition: sw_buddy.h:94
void * heap_start
Definition: sw_buddy.h:90
unsigned int count
Definition: sw_buddy.h:67
struct sw_free_area free_area[BINS_MAX_ORDER]
Definition: sw_buddy.h:92
void buddy_free(int index, void *ptr)
Frees the allocated memory block at the given heap index and address ptr.
Definition: sw_buddy.c:555
unsigned int heap_size
Definition: sw_buddy.h:45