File size: 3,510 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef _ALLOC_H_
#define _ALLOC_H_
/*
 * alloc supplies three ingredients to the test framework that are all
 * related to the support of dynamic memory allocation.
 *
 * The first is a set of alloc function wrappers for malloc and its
 * friends. Using wrappers allows test code and common code to use the
 * same interface for memory allocation at all stages, even though the
 * implementations may change with the stage, e.g. pre/post paging.
 *
 * The second is a set of implementations for the alloc function
 * interfaces. These implementations are named early_*, as they can be
 * used almost immediately by the test framework.
 *
 * The third is a very simple physical memory allocator, which the
 * early_* alloc functions build on.
 *
 * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com>
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.
 */
#include "libcflat.h"

struct alloc_ops {
	void *(*malloc)(size_t size);
	void *(*calloc)(size_t nmemb, size_t size);
	void (*free)(void *ptr);
	void *(*memalign)(size_t alignment, size_t size);
};

/*
 * alloc_ops is initialized to early_alloc_ops
 */
extern struct alloc_ops *alloc_ops;

static inline void *malloc(size_t size)
{
	assert(alloc_ops && alloc_ops->malloc);
	return alloc_ops->malloc(size);
}

static inline void *calloc(size_t nmemb, size_t size)
{
	assert(alloc_ops && alloc_ops->calloc);
	return alloc_ops->calloc(nmemb, size);
}

static inline void free(void *ptr)
{
	assert(alloc_ops && alloc_ops->free);
	alloc_ops->free(ptr);
}

static inline void *memalign(size_t alignment, size_t size)
{
	assert(alloc_ops && alloc_ops->memalign);
	return alloc_ops->memalign(alignment, size);
}

/*
 * phys_alloc is a very simple allocator which allows physical memory
 * to be partitioned into regions until all memory is allocated.
 *
 * Note: This is such a simple allocator that there is no way to free
 * a region. For more complicated memory management a single region
 * can be allocated, but then have its memory managed by a more
 * sophisticated allocator, e.g. a page allocator.
 */
#define DEFAULT_MINIMUM_ALIGNMENT 32

/*
 * phys_alloc_init creates the initial free memory region of size @size
 * at @base. The minimum alignment is set to DEFAULT_MINIMUM_ALIGNMENT.
 */
extern void phys_alloc_init(phys_addr_t base, phys_addr_t size);

/*
 * phys_alloc_set_minimum_alignment sets the minimum alignment to
 * @align.
 */
extern void phys_alloc_set_minimum_alignment(phys_addr_t align);

/*
 * phys_alloc_aligned returns the base address of a region of size @size,
 * where the address is aligned to @align, or INVALID_PHYS_ADDR if there
 * isn't enough free memory to satisfy the request.
 */
extern phys_addr_t phys_alloc_aligned(phys_addr_t size, phys_addr_t align);

/*
 * phys_zalloc_aligned is like phys_alloc_aligned, but zeros the memory
 * before returning the address.
 */
extern phys_addr_t phys_zalloc_aligned(phys_addr_t size, phys_addr_t align);

/*
 * phys_alloc returns the base address of a region of size @size, or
 * INVALID_PHYS_ADDR if there isn't enough free memory to satisfy the
 * request.
 */
extern phys_addr_t phys_alloc(phys_addr_t size);

/*
 * phys_zalloc is like phys_alloc, but zeros the memory before returning.
 */
extern phys_addr_t phys_zalloc(phys_addr_t size);

/*
 * phys_alloc_show outputs all currently allocated regions with the
 * following format
 *   <start_addr>-<end_addr> [<USED|FREE>]
 */
extern void phys_alloc_show(void);

#endif /* _ALLOC_H_ */