Kenton Varda | 889204f | 2014-06-20 00:29:27 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors |
| 2 | // Licensed under the MIT License: |
Kenton Varda | f9ee42c | 2013-05-30 03:57:48 -0700 | [diff] [blame] | 3 | // |
Kenton Varda | 889204f | 2014-06-20 00:29:27 -0700 | [diff] [blame] | 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | // of this software and associated documentation files (the "Software"), to deal |
| 6 | // in the Software without restriction, including without limitation the rights |
| 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | // copies of the Software, and to permit persons to whom the Software is |
| 9 | // furnished to do so, subject to the following conditions: |
Kenton Varda | f9ee42c | 2013-05-30 03:57:48 -0700 | [diff] [blame] | 10 | // |
Kenton Varda | 889204f | 2014-06-20 00:29:27 -0700 | [diff] [blame] | 11 | // The above copyright notice and this permission notice shall be included in |
| 12 | // all copies or substantial portions of the Software. |
Kenton Varda | f9ee42c | 2013-05-30 03:57:48 -0700 | [diff] [blame] | 13 | // |
Kenton Varda | 889204f | 2014-06-20 00:29:27 -0700 | [diff] [blame] | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | // THE SOFTWARE. |
Kenton Varda | f9ee42c | 2013-05-30 03:57:48 -0700 | [diff] [blame] | 21 | |
| 22 | #include "array.h" |
Kenton Varda | 21a508b | 2013-08-22 23:01:23 -0700 | [diff] [blame] | 23 | #include "exception.h" |
Kenton Varda | f9ee42c | 2013-05-30 03:57:48 -0700 | [diff] [blame] | 24 | |
| 25 | namespace kj { |
Kenton Varda | 8e1d1e5 | 2013-07-09 21:38:20 -0700 | [diff] [blame] | 26 | |
| 27 | void ExceptionSafeArrayUtil::construct(size_t count, void (*constructElement)(void*)) { |
| 28 | while (count > 0) { |
| 29 | constructElement(pos); |
| 30 | pos += elementSize; |
| 31 | ++constructedElementCount; |
| 32 | --count; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void ExceptionSafeArrayUtil::destroyAll() { |
| 37 | while (constructedElementCount > 0) { |
| 38 | pos -= elementSize; |
| 39 | --constructedElementCount; |
| 40 | destroyElement(pos); |
| 41 | } |
| 42 | } |
| 43 | |
Kenton Varda | 719eb09 | 2013-07-17 15:58:30 -0700 | [diff] [blame] | 44 | const DestructorOnlyArrayDisposer DestructorOnlyArrayDisposer::instance = |
| 45 | DestructorOnlyArrayDisposer(); |
| 46 | |
| 47 | void DestructorOnlyArrayDisposer::disposeImpl( |
| 48 | void* firstElement, size_t elementSize, size_t elementCount, |
| 49 | size_t capacity, void (*destroyElement)(void*)) const { |
| 50 | if (destroyElement != nullptr) { |
| 51 | ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement); |
| 52 | guard.destroyAll(); |
| 53 | } |
| 54 | } |
| 55 | |
Kenton Varda | de7f266 | 2013-08-01 04:32:56 -0700 | [diff] [blame] | 56 | const NullArrayDisposer NullArrayDisposer::instance = NullArrayDisposer(); |
| 57 | |
| 58 | void NullArrayDisposer::disposeImpl( |
| 59 | void* firstElement, size_t elementSize, size_t elementCount, |
| 60 | size_t capacity, void (*destroyElement)(void*)) const {} |
| 61 | |
Kenton Varda | aa65499 | 2013-06-06 03:50:25 -0700 | [diff] [blame] | 62 | namespace _ { // private |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 63 | |
Kenton Varda | 21a508b | 2013-08-22 23:01:23 -0700 | [diff] [blame] | 64 | struct AutoDeleter { |
| 65 | void* ptr; |
| 66 | inline void* release() { void* result = ptr; ptr = nullptr; return result; } |
| 67 | inline AutoDeleter(void* ptr): ptr(ptr) {} |
| 68 | inline ~AutoDeleter() { operator delete(ptr); } |
| 69 | }; |
| 70 | |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 71 | void* HeapArrayDisposer::allocateImpl(size_t elementSize, size_t elementCount, size_t capacity, |
| 72 | void (*constructElement)(void*), |
| 73 | void (*destroyElement)(void*)) { |
Kenton Varda | 21a508b | 2013-08-22 23:01:23 -0700 | [diff] [blame] | 74 | AutoDeleter result(operator new(elementSize * capacity)); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 75 | |
| 76 | if (constructElement == nullptr) { |
| 77 | // Nothing to do. |
| 78 | } else if (destroyElement == nullptr) { |
Kenton Varda | 21a508b | 2013-08-22 23:01:23 -0700 | [diff] [blame] | 79 | byte* pos = reinterpret_cast<byte*>(result.ptr); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 80 | while (elementCount > 0) { |
| 81 | constructElement(pos); |
| 82 | pos += elementSize; |
| 83 | --elementCount; |
| 84 | } |
| 85 | } else { |
Kenton Varda | 21a508b | 2013-08-22 23:01:23 -0700 | [diff] [blame] | 86 | ExceptionSafeArrayUtil guard(result.ptr, elementSize, 0, destroyElement); |
Kenton Varda | 8e1d1e5 | 2013-07-09 21:38:20 -0700 | [diff] [blame] | 87 | guard.construct(elementCount, constructElement); |
| 88 | guard.release(); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Kenton Varda | 21a508b | 2013-08-22 23:01:23 -0700 | [diff] [blame] | 91 | return result.release(); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void HeapArrayDisposer::disposeImpl( |
| 95 | void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, |
| 96 | void (*destroyElement)(void*)) const { |
| 97 | // Note that capacity is ignored since operator delete() doesn't care about it. |
Kenton Varda | 21a508b | 2013-08-22 23:01:23 -0700 | [diff] [blame] | 98 | AutoDeleter deleter(firstElement); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 99 | |
Kenton Varda | 21a508b | 2013-08-22 23:01:23 -0700 | [diff] [blame] | 100 | if (destroyElement != nullptr) { |
Kenton Varda | 8e1d1e5 | 2013-07-09 21:38:20 -0700 | [diff] [blame] | 101 | ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 102 | guard.destroyAll(); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | const HeapArrayDisposer HeapArrayDisposer::instance = HeapArrayDisposer(); |
| 107 | |
Kenton Varda | aa65499 | 2013-06-06 03:50:25 -0700 | [diff] [blame] | 108 | } // namespace _ (private) |
Kenton Varda | f9ee42c | 2013-05-30 03:57:48 -0700 | [diff] [blame] | 109 | } // namespace kj |