Kenton Varda | f9ee42c | 2013-05-30 03:57:48 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013, Kenton Varda <temporal@gmail.com> |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 14 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 17 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 20 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | |
| 24 | #include "array.h" |
| 25 | |
| 26 | namespace kj { |
Kenton Varda | 8e1d1e5 | 2013-07-09 21:38:20 -0700 | [diff] [blame] | 27 | |
| 28 | void ExceptionSafeArrayUtil::construct(size_t count, void (*constructElement)(void*)) { |
| 29 | while (count > 0) { |
| 30 | constructElement(pos); |
| 31 | pos += elementSize; |
| 32 | ++constructedElementCount; |
| 33 | --count; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void ExceptionSafeArrayUtil::destroyAll() { |
| 38 | while (constructedElementCount > 0) { |
| 39 | pos -= elementSize; |
| 40 | --constructedElementCount; |
| 41 | destroyElement(pos); |
| 42 | } |
| 43 | } |
| 44 | |
Kenton Varda | 719eb09 | 2013-07-17 15:58:30 -0700 | [diff] [blame] | 45 | const DestructorOnlyArrayDisposer DestructorOnlyArrayDisposer::instance = |
| 46 | DestructorOnlyArrayDisposer(); |
| 47 | |
| 48 | void DestructorOnlyArrayDisposer::disposeImpl( |
| 49 | void* firstElement, size_t elementSize, size_t elementCount, |
| 50 | size_t capacity, void (*destroyElement)(void*)) const { |
| 51 | if (destroyElement != nullptr) { |
| 52 | ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement); |
| 53 | guard.destroyAll(); |
| 54 | } |
| 55 | } |
| 56 | |
Kenton Varda | de7f266 | 2013-08-01 04:32:56 -0700 | [diff] [blame^] | 57 | const NullArrayDisposer NullArrayDisposer::instance = NullArrayDisposer(); |
| 58 | |
| 59 | void NullArrayDisposer::disposeImpl( |
| 60 | void* firstElement, size_t elementSize, size_t elementCount, |
| 61 | size_t capacity, void (*destroyElement)(void*)) const {} |
| 62 | |
Kenton Varda | aa65499 | 2013-06-06 03:50:25 -0700 | [diff] [blame] | 63 | namespace _ { // private |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 64 | |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 65 | void* HeapArrayDisposer::allocateImpl(size_t elementSize, size_t elementCount, size_t capacity, |
| 66 | void (*constructElement)(void*), |
| 67 | void (*destroyElement)(void*)) { |
| 68 | void* result = operator new(elementSize * capacity); |
| 69 | |
| 70 | if (constructElement == nullptr) { |
| 71 | // Nothing to do. |
| 72 | } else if (destroyElement == nullptr) { |
| 73 | byte* pos = reinterpret_cast<byte*>(result); |
| 74 | while (elementCount > 0) { |
| 75 | constructElement(pos); |
| 76 | pos += elementSize; |
| 77 | --elementCount; |
| 78 | } |
| 79 | } else { |
Kenton Varda | 8e1d1e5 | 2013-07-09 21:38:20 -0700 | [diff] [blame] | 80 | ExceptionSafeArrayUtil guard(result, elementSize, 0, destroyElement); |
| 81 | guard.construct(elementCount, constructElement); |
| 82 | guard.release(); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | void HeapArrayDisposer::disposeImpl( |
| 89 | void* firstElement, size_t elementSize, size_t elementCount, size_t capacity, |
| 90 | void (*destroyElement)(void*)) const { |
| 91 | // Note that capacity is ignored since operator delete() doesn't care about it. |
| 92 | |
| 93 | if (destroyElement == nullptr) { |
| 94 | operator delete(firstElement); |
| 95 | } else { |
Kenton Varda | 8e1d1e5 | 2013-07-09 21:38:20 -0700 | [diff] [blame] | 96 | KJ_DEFER(operator delete(firstElement)); |
| 97 | ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 98 | guard.destroyAll(); |
Kenton Varda | 4958d3a | 2013-05-30 21:12:54 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | const HeapArrayDisposer HeapArrayDisposer::instance = HeapArrayDisposer(); |
| 103 | |
Kenton Varda | aa65499 | 2013-06-06 03:50:25 -0700 | [diff] [blame] | 104 | } // namespace _ (private) |
Kenton Varda | f9ee42c | 2013-05-30 03:57:48 -0700 | [diff] [blame] | 105 | } // namespace kj |