Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | /** |
| 19 | * Runtime functions. This includes common runtime library functions we |
| 20 | * may need portably. |
| 21 | * @file ucommon/cpr.h |
| 22 | * @author David Sugar <dyfet@gnutelephony.org> |
| 23 | */ |
| 24 | |
| 25 | #ifndef _UCOMMON_CONFIG_H_ |
| 26 | #include <ucommon/platform.h> |
| 27 | #endif |
| 28 | |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame^] | 29 | |
| 30 | #include <new> |
| 31 | |
Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 32 | #ifndef _UCOMMON_CPR_H_ |
| 33 | #define _UCOMMON_CPR_H_ |
| 34 | |
| 35 | #ifdef _MSWINDOWS_ |
| 36 | |
| 37 | extern "C" { |
| 38 | __EXPORT int cpr_setenv(const char *s, const char *v, int p); |
| 39 | |
| 40 | inline int setenv(const char *s, const char *v, int overwrite) |
| 41 | {return cpr_setenv(s, v, overwrite);} |
| 42 | } |
| 43 | |
| 44 | #endif |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Function to handle runtime errors. When using the standard C library, |
| 49 | * runtime errors are handled by a simple abort. When using the stdc++ |
| 50 | * library with stdexcept, then std::runtime_error will be thrown. |
| 51 | * @param text of runtime error. |
| 52 | */ |
| 53 | __EXPORT void cpr_runtime_error(const char *text); |
| 54 | |
| 55 | /** |
| 56 | * Portable memory allocation helper function. Handles out of heap error |
| 57 | * as a runtime error. |
| 58 | * @param size of memory block to allocate from heap. |
| 59 | * @return memory address of allocated heap space. |
| 60 | */ |
| 61 | extern "C" __EXPORT void *cpr_memalloc(size_t size) __MALLOC; |
| 62 | |
| 63 | /** |
| 64 | * Portable memory placement helper function. This is used to process |
| 65 | * "placement" new operators where a new object is constructed over a |
| 66 | * pre-allocated area of memory. This handles invalid values through |
| 67 | * runtime error. |
| 68 | * @param size of object being constructed. |
| 69 | * @param address where the object is being placed. |
| 70 | * @param known size of the location we are constructing the object in. |
| 71 | */ |
| 72 | extern "C" __EXPORT void *cpr_memassign(size_t size, caddr_t address, size_t known) __MALLOC; |
| 73 | |
| 74 | /** |
| 75 | * Portable swap code. |
| 76 | * @param mem1 to swap. |
| 77 | * @param mem2 to swap. |
| 78 | * @param size of swap area. |
| 79 | */ |
| 80 | extern "C" __EXPORT void cpr_memswap(void *mem1, void *mem2, size_t size); |
| 81 | |
| 82 | #ifndef _UCOMMON_EXTENDED_ |
| 83 | /** |
| 84 | * Our generic new operator. Uses our heap memory allocator. |
| 85 | * @param size of object being constructed. |
| 86 | * @return memory allocated from heap. |
| 87 | */ |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame^] | 88 | inline void *operator new(size_t size) throw(std::bad_alloc) |
Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 89 | {return cpr_memalloc(size);} |
| 90 | |
| 91 | /** |
| 92 | * Our generic new array operator. Uses our heap memory allocator. |
| 93 | * @param size of memory needed for object array. |
| 94 | * @return memory allocated from heap. |
| 95 | */ |
Alexandre Lision | 907ed2e | 2014-02-04 10:33:09 -0500 | [diff] [blame^] | 96 | inline void *operator new[](size_t size) throw(std::bad_alloc) |
Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 97 | {return cpr_memalloc(size);} |
| 98 | #endif |
| 99 | |
| 100 | #ifndef _UCOMMON_EXTENDED_ |
| 101 | /** |
| 102 | * A placement new array operator where we assume the size of memory is good. |
| 103 | * We construct the array at a specified place in memory which we assume is |
| 104 | * valid for our needs. |
| 105 | * @param size of memory needed for object array. |
| 106 | * @param address where to place object array. |
| 107 | * @return memory we placed object array. |
| 108 | */ |
| 109 | inline void *operator new[](size_t size, caddr_t address) |
| 110 | {return cpr_memassign(size, address, size);} |
| 111 | |
| 112 | /** |
| 113 | * A placement new array operator where we know the allocated size. We |
| 114 | * find out how much memory is needed by the new and can prevent arrayed |
| 115 | * objects from exceeding the available space we are placing the object. |
| 116 | * @param size of memory needed for object array. |
| 117 | * @param address where to place object array. |
| 118 | * @param known size of location we are placing array. |
| 119 | * @return memory we placed object array. |
| 120 | */ |
| 121 | inline void *operator new[](size_t size, caddr_t address, size_t known) |
| 122 | {return cpr_memassign(size, address, known);} |
| 123 | #endif |
| 124 | |
| 125 | /** |
| 126 | * Overdraft new to allocate extra memory for object from heap. This is |
| 127 | * used for objects that must have a known class size but store extra data |
| 128 | * behind the class. The last member might be an unsized or 0 element |
| 129 | * array, and the actual size needed from the heap is hence not the size of |
| 130 | * the class itself but is known by the routine allocating the object. |
| 131 | * @param size of object. |
| 132 | * @param extra heap space needed for data. |
| 133 | */ |
| 134 | inline void *operator new(size_t size, size_t extra) |
| 135 | {return cpr_memalloc(size + extra);} |
| 136 | |
| 137 | /** |
| 138 | * A placement new operator where we assume the size of memory is good. |
| 139 | * We construct the object at a specified place in memory which we assume is |
| 140 | * valid for our needs. |
| 141 | * @param size of memory needed for object. |
| 142 | * @param address where to place object. |
| 143 | * @return memory we placed object. |
| 144 | */ |
| 145 | inline void *operator new(size_t size, caddr_t address) |
| 146 | {return cpr_memassign(size, address, size);} |
| 147 | |
| 148 | /** |
| 149 | * A placement new operator where we know the allocated size. We |
| 150 | * find out how much memory is needed by the new and can prevent the object |
| 151 | * from exceeding the available space we are placing the object. |
| 152 | * @param size of memory needed for object. |
| 153 | * @param address where to place object. |
| 154 | * @param known size of location we are placing object. |
| 155 | * @return memory we placed object. |
| 156 | */ |
| 157 | |
| 158 | inline void *operator new(size_t size, caddr_t address, size_t known) |
| 159 | {return cpr_memassign(size, address, known);} |
| 160 | |
| 161 | #ifndef _UCOMMON_EXTENDED_ |
| 162 | |
| 163 | |
| 164 | #ifdef __GNUC__ |
| 165 | extern "C" __EXPORT void __cxa_pure_virtual(void); |
| 166 | #endif |
| 167 | #endif |
| 168 | |
| 169 | extern "C" { |
| 170 | __EXPORT uint16_t lsb_getshort(uint8_t *b); |
| 171 | __EXPORT uint32_t lsb_getlong(uint8_t *b); |
| 172 | __EXPORT uint16_t msb_getshort(uint8_t *b); |
| 173 | __EXPORT uint32_t msb_getlong(uint8_t *b); |
| 174 | |
| 175 | __EXPORT void lsb_setshort(uint8_t *b, uint16_t v); |
| 176 | __EXPORT void lsb_setlong(uint8_t *b, uint32_t v); |
| 177 | __EXPORT void msb_setshort(uint8_t *b, uint16_t v); |
| 178 | __EXPORT void msb_setlong(uint8_t *b, uint32_t v); |
| 179 | } |
| 180 | |
| 181 | #endif |