3 /*****************************************************************************
5 (c) Cambridge Silicon Radio Limited 2010
6 All rights reserved and confidential information of CSR
8 Refer to LICENSE.txt included with this source for details
11 *****************************************************************************/
13 #include "csr_types.h"
20 #ifndef CSR_PMEM_DEBUG_ENABLE
21 /*****************************************************************************
27 This function will allocate a contiguous block of memory of at least
28 the specified size in bytes and return a pointer to the allocated
29 memory. This function is not allowed to return NULL. A size of 0 is a
30 valid request, and a unique and valid (not NULL) pointer must be
31 returned in this case.
34 size - Size of memory requested. Note that a size of 0 is valid.
37 Pointer to allocated memory.
39 *****************************************************************************/
41 void *CsrPmemAllocDebug(CsrSize size
,
42 const char *file
, u32 line
);
43 #define CsrPmemAlloc(sz) CsrPmemAllocDebug((sz), __FILE__, __LINE__)
45 void *CsrPmemAlloc(CsrSize size
);
49 /*****************************************************************************
55 This function will deallocate a previously allocated block of memory.
58 ptr - Pointer to allocated memory.
60 *****************************************************************************/
61 void CsrPmemFree(void *ptr
);
64 /*****************************************************************************
70 This function is equivalent to CsrPmemAlloc, but the allocated memory
71 is initialised to zero.
74 size - Size of memory requested. Note that a size of 0 is valid.
77 Pointer to allocated memory.
79 *****************************************************************************/
80 #define CsrPmemZalloc(s) (CsrMemSet(CsrPmemAlloc(s), 0x00, (s)))
83 /*****************************************************************************
89 Type-safe wrappers for CsrPmemAlloc and CsrPmemZalloc, for allocating
90 single instances of a specified and named type.
95 *****************************************************************************/
96 #define pnew(t) ((t *) (CsrPmemAlloc(sizeof(t))))
97 #define zpnew(t) ((t *) (CsrPmemZalloc(sizeof(t))))
100 /*----------------------------------------------------------------------------*
101 * Csr Pmem Debug code. Allows custom callbacks on CsrPmemAlloc and CsrPmemFree
102 *----------------------------------------------------------------------------*/
103 #ifdef CSR_PMEM_DEBUG_ENABLE
105 typedef u8 CsrPmemDebugAllocType
;
106 #define CSR_PMEM_DEBUG_TYPE_PMEM_ALLOC 1
107 #define CSR_PMEM_DEBUG_TYPE_MEM_ALLOC 2
108 #define CSR_PMEM_DEBUG_TYPE_MEM_CALLOC 3
109 #define CSR_PMEM_DEBUG_TYPE_MEM_ALLOC_DMA 4
111 typedef void (CsrPmemDebugOnAlloc
)(void *ptr
, void *userptr
, CsrSize size
, CsrPmemDebugAllocType type
, const char* file
, u32 line
);
112 typedef void (CsrPmemDebugOnFree
)(void *ptr
, void *userptr
, CsrPmemDebugAllocType type
, const char* file
, u32 line
);
114 /*----------------------------------------------------------------------------*
116 * CsrPmemInstallHooks
119 * Install debug hooks for memory allocation
120 * Use NULL values to uninstall the hooks
121 * headSize = The number of extra bytes to allocate in the head of the Allocated buffer
122 * footSize = The number of extra bytes to allocate in the end of the Allocated buffer
127 *----------------------------------------------------------------------------*/
128 void CsrPmemDebugInstallHooks(u8 headSize
, u8 endSize
, CsrPmemDebugOnAlloc
*onAllocCallback
, CsrPmemDebugOnFree
*onFreeCallback
);
130 void *CsrPmemDebugAlloc(CsrSize size
, CsrPmemDebugAllocType type
, const char* file
, u32 line
);
131 #define CsrPmemAlloc(size) CsrPmemDebugAlloc(size, CSR_PMEM_DEBUG_TYPE_PMEM_ALLOC, __FILE__, __LINE__)
133 void CsrPmemDebugFree(void *ptr
, CsrPmemDebugAllocType type
, const char* file
, u32 line
);
134 #define CsrPmemFree(ptr) CsrPmemDebugFree(ptr, CSR_PMEM_DEBUG_TYPE_PMEM_ALLOC, __FILE__, __LINE__)