blob: 3ac3d489c832fabeaa617f0c4a1ba4317ab91a2c [file] [log] [blame]
Kenton Varda889204f2014-06-20 00:29:27 -07001// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2// Licensed under the MIT License:
Kenton Vardaf9ee42c2013-05-30 03:57:48 -07003//
Kenton Varda889204f2014-06-20 00:29:27 -07004// 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 Vardaf9ee42c2013-05-30 03:57:48 -070010//
Kenton Varda889204f2014-06-20 00:29:27 -070011// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
Kenton Vardaf9ee42c2013-05-30 03:57:48 -070013//
Kenton Varda889204f2014-06-20 00:29:27 -070014// 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 Vardaf9ee42c2013-05-30 03:57:48 -070021
22#include "array.h"
Kenton Varda21a508b2013-08-22 23:01:23 -070023#include "exception.h"
Kenton Vardaf9ee42c2013-05-30 03:57:48 -070024
25namespace kj {
Kenton Varda8e1d1e52013-07-09 21:38:20 -070026
27void 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
36void ExceptionSafeArrayUtil::destroyAll() {
37 while (constructedElementCount > 0) {
38 pos -= elementSize;
39 --constructedElementCount;
40 destroyElement(pos);
41 }
42}
43
Kenton Varda719eb092013-07-17 15:58:30 -070044const DestructorOnlyArrayDisposer DestructorOnlyArrayDisposer::instance =
45 DestructorOnlyArrayDisposer();
46
47void 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 Vardade7f2662013-08-01 04:32:56 -070056const NullArrayDisposer NullArrayDisposer::instance = NullArrayDisposer();
57
58void NullArrayDisposer::disposeImpl(
59 void* firstElement, size_t elementSize, size_t elementCount,
60 size_t capacity, void (*destroyElement)(void*)) const {}
61
Kenton Vardaaa654992013-06-06 03:50:25 -070062namespace _ { // private
Kenton Varda4958d3a2013-05-30 21:12:54 -070063
Kenton Varda21a508b2013-08-22 23:01:23 -070064struct 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 Varda4958d3a2013-05-30 21:12:54 -070071void* HeapArrayDisposer::allocateImpl(size_t elementSize, size_t elementCount, size_t capacity,
72 void (*constructElement)(void*),
73 void (*destroyElement)(void*)) {
Kenton Varda21a508b2013-08-22 23:01:23 -070074 AutoDeleter result(operator new(elementSize * capacity));
Kenton Varda4958d3a2013-05-30 21:12:54 -070075
76 if (constructElement == nullptr) {
77 // Nothing to do.
78 } else if (destroyElement == nullptr) {
Kenton Varda21a508b2013-08-22 23:01:23 -070079 byte* pos = reinterpret_cast<byte*>(result.ptr);
Kenton Varda4958d3a2013-05-30 21:12:54 -070080 while (elementCount > 0) {
81 constructElement(pos);
82 pos += elementSize;
83 --elementCount;
84 }
85 } else {
Kenton Varda21a508b2013-08-22 23:01:23 -070086 ExceptionSafeArrayUtil guard(result.ptr, elementSize, 0, destroyElement);
Kenton Varda8e1d1e52013-07-09 21:38:20 -070087 guard.construct(elementCount, constructElement);
88 guard.release();
Kenton Varda4958d3a2013-05-30 21:12:54 -070089 }
90
Kenton Varda21a508b2013-08-22 23:01:23 -070091 return result.release();
Kenton Varda4958d3a2013-05-30 21:12:54 -070092}
93
94void 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 Varda21a508b2013-08-22 23:01:23 -070098 AutoDeleter deleter(firstElement);
Kenton Varda4958d3a2013-05-30 21:12:54 -070099
Kenton Varda21a508b2013-08-22 23:01:23 -0700100 if (destroyElement != nullptr) {
Kenton Varda8e1d1e52013-07-09 21:38:20 -0700101 ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement);
Kenton Varda4958d3a2013-05-30 21:12:54 -0700102 guard.destroyAll();
Kenton Varda4958d3a2013-05-30 21:12:54 -0700103 }
104}
105
106const HeapArrayDisposer HeapArrayDisposer::instance = HeapArrayDisposer();
107
Kenton Vardaaa654992013-06-06 03:50:25 -0700108} // namespace _ (private)
Kenton Vardaf9ee42c2013-05-30 03:57:48 -0700109} // namespace kj