| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | /*============================================================================= |
| 29 | VideoCore OS Abstraction Layer - memory alloc implementation |
| 30 | =============================================================================*/ |
| 31 | |
| 32 | #include "interface/vcos/vcos.h" |
| 33 | |
| 34 | #ifndef _vcos_platform_malloc |
| 35 | #include <stdlib.h> |
| 36 | #define _vcos_platform_malloc malloc |
| 37 | #define _vcos_platform_free free |
| 38 | #endif |
| 39 | |
| 40 | typedef struct { |
| 41 | uint32_t ; |
| 42 | uint32_t ; |
| 43 | const char *; |
| 44 | void *; |
| 45 | } ; |
| 46 | |
| 47 | |
| 48 | #define MIN_ALIGN sizeof(MALLOC_HEADER_T) |
| 49 | |
| 50 | #define GUARDWORDHEAP 0xa55a5aa5 |
| 51 | |
| 52 | void *vcos_generic_mem_alloc_aligned(VCOS_UNSIGNED size, VCOS_UNSIGNED align, const char *desc) |
| 53 | { |
| 54 | int local_align = align == 0 ? 1 : align; |
| 55 | int required_size = size + local_align + sizeof(MALLOC_HEADER_T); |
| 56 | void *ptr = _vcos_platform_malloc(required_size); |
| 57 | void *ret = NULL; |
| 58 | MALLOC_HEADER_T *h; |
| 59 | |
| 60 | if (ptr) |
| 61 | { |
| 62 | ret = (void *)VCOS_ALIGN_UP(((char *)ptr)+sizeof(MALLOC_HEADER_T), local_align); |
| 63 | h = ((MALLOC_HEADER_T *)ret)-1; |
| 64 | h->size = size; |
| 65 | h->description = desc; |
| 66 | h->guardword = GUARDWORDHEAP; |
| 67 | h->ptr = ptr; |
| 68 | } |
| 69 | |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | void *vcos_generic_mem_alloc(VCOS_UNSIGNED size, const char *desc) |
| 74 | { |
| 75 | return vcos_generic_mem_alloc_aligned(size,MIN_ALIGN,desc); |
| 76 | } |
| 77 | |
| 78 | void *vcos_generic_mem_calloc(VCOS_UNSIGNED count, VCOS_UNSIGNED sz, const char *desc) |
| 79 | { |
| 80 | uint32_t size = count*sz; |
| 81 | void *ptr = vcos_generic_mem_alloc_aligned(size,MIN_ALIGN,desc); |
| 82 | if (ptr) |
| 83 | { |
| 84 | memset(ptr, 0, size); |
| 85 | } |
| 86 | return ptr; |
| 87 | } |
| 88 | |
| 89 | void vcos_generic_mem_free(void *ptr) |
| 90 | { |
| 91 | MALLOC_HEADER_T *h; |
| 92 | if (! ptr) return; |
| 93 | |
| 94 | h = ((MALLOC_HEADER_T *)ptr)-1; |
| 95 | vcos_assert(h->guardword == GUARDWORDHEAP); |
| 96 | _vcos_platform_free(h->ptr); |
| 97 | } |
| 98 | |
| 99 | |