| 1 | /* |
| 2 | * Copyright (C) 2013-2019 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This file is part of GNU lightning. |
| 5 | * |
| 6 | * GNU lightning is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU Lesser General Public License as published |
| 8 | * by the Free Software Foundation; either version 3, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * GNU lightning is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 14 | * License for more details. |
| 15 | * |
| 16 | * Authors: |
| 17 | * Paulo Cesar Pereira de Andrade |
| 18 | */ |
| 19 | |
| 20 | #include <lightning.h> |
| 21 | #include <lightning/jit_private.h> |
| 22 | #ifdef _WIN32 |
| 23 | # include <mman.h> |
| 24 | #else |
| 25 | # include <sys/mman.h> |
| 26 | #endif |
| 27 | |
| 28 | /* |
| 29 | * Prototypes |
| 30 | */ |
| 31 | static void *jit_default_alloc_func(size_t); |
| 32 | static void *jit_default_realloc_func(void*, size_t); |
| 33 | static void jit_default_free_func(void *); |
| 34 | |
| 35 | /* |
| 36 | * Initialization |
| 37 | */ |
| 38 | static jit_alloc_func_ptr jit_alloc_ptr = jit_default_alloc_func; |
| 39 | static jit_realloc_func_ptr jit_realloc_ptr = jit_default_realloc_func; |
| 40 | static jit_free_func_ptr jit_free_ptr = jit_default_free_func; |
| 41 | |
| 42 | /* |
| 43 | * Implementation |
| 44 | */ |
| 45 | jit_pointer_t |
| 46 | jit_memcpy(jit_pointer_t dst, const void * src, jit_word_t size) |
| 47 | { |
| 48 | if (size) |
| 49 | return (memcpy(dst, src, size)); |
| 50 | return (dst); |
| 51 | } |
| 52 | |
| 53 | jit_pointer_t |
| 54 | jit_memmove(jit_pointer_t dst, const void *src , jit_word_t size) |
| 55 | { |
| 56 | if (size) |
| 57 | return (memmove(dst, src, size)); |
| 58 | return (dst); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | jit_set_memory_functions(jit_alloc_func_ptr alloc_ptr, |
| 63 | jit_realloc_func_ptr realloc_ptr, |
| 64 | jit_free_func_ptr free_ptr) |
| 65 | { |
| 66 | if (alloc_ptr == NULL) |
| 67 | alloc_ptr = jit_default_alloc_func; |
| 68 | if (realloc_ptr == NULL) |
| 69 | realloc_ptr = jit_default_realloc_func; |
| 70 | if (free_ptr == NULL) |
| 71 | free_ptr = jit_default_free_func; |
| 72 | jit_alloc_ptr = alloc_ptr; |
| 73 | jit_realloc_ptr = realloc_ptr; |
| 74 | jit_free_ptr = free_ptr; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | jit_get_memory_functions(jit_alloc_func_ptr *alloc_ptr, |
| 79 | jit_realloc_func_ptr *realloc_ptr, |
| 80 | jit_free_func_ptr *free_ptr) |
| 81 | { |
| 82 | *alloc_ptr = jit_alloc_ptr; |
| 83 | *realloc_ptr = jit_realloc_ptr; |
| 84 | *free_ptr = jit_free_ptr; |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | jit_alloc(jit_pointer_t *ptr, jit_word_t size) |
| 89 | { |
| 90 | *ptr = (*jit_alloc_ptr)(size); |
| 91 | memset(*ptr, 0, size); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | jit_realloc(jit_pointer_t *ptr, jit_word_t old_size, jit_word_t new_size) |
| 96 | { |
| 97 | *ptr = (*jit_realloc_ptr)(*ptr, new_size); |
| 98 | if (old_size < new_size) |
| 99 | memset((jit_int8_t*)*ptr + old_size, 0, new_size - old_size); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | jit_free(jit_pointer_t *ptr) |
| 104 | { |
| 105 | if (*ptr) { |
| 106 | (*jit_free_ptr)(*ptr); |
| 107 | *ptr = NULL; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static void * |
| 112 | jit_default_alloc_func(size_t size) |
| 113 | { |
| 114 | return (malloc(size)); |
| 115 | } |
| 116 | |
| 117 | static void * |
| 118 | jit_default_realloc_func(void *ptr, size_t size) |
| 119 | { |
| 120 | return (realloc(ptr, size)); |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | jit_default_free_func(void *ptr) |
| 125 | { |
| 126 | free(ptr); |
| 127 | } |