array.c++ 3.79 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
21 22

#include "array.h"
23
#include "exception.h"
24 25

namespace kj {
Kenton Varda's avatar
Kenton Varda committed
26

27 28 29 30 31 32
void ExceptionSafeArrayUtil::construct(size_t count, void (*constructElement)(void*)) {
  while (count > 0) {
    constructElement(pos);
    pos += elementSize;
    ++constructedElementCount;
    --count;
Kenton Varda's avatar
Kenton Varda committed
33
  }
34
}
Kenton Varda's avatar
Kenton Varda committed
35

36 37 38 39 40
void ExceptionSafeArrayUtil::destroyAll() {
  while (constructedElementCount > 0) {
    pos -= elementSize;
    --constructedElementCount;
    destroyElement(pos);
Kenton Varda's avatar
Kenton Varda committed
41
  }
42 43
}

44 45 46 47 48 49 50 51 52 53 54 55
const DestructorOnlyArrayDisposer DestructorOnlyArrayDisposer::instance =
    DestructorOnlyArrayDisposer();

void DestructorOnlyArrayDisposer::disposeImpl(
    void* firstElement, size_t elementSize, size_t elementCount,
    size_t capacity, void (*destroyElement)(void*)) const {
  if (destroyElement != nullptr) {
    ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement);
    guard.destroyAll();
  }
}

56 57 58 59 60 61
const NullArrayDisposer NullArrayDisposer::instance = NullArrayDisposer();

void NullArrayDisposer::disposeImpl(
    void* firstElement, size_t elementSize, size_t elementCount,
    size_t capacity, void (*destroyElement)(void*)) const {}

62
namespace _ {  // private
Kenton Varda's avatar
Kenton Varda committed
63

64 65 66 67 68 69 70
struct AutoDeleter {
  void* ptr;
  inline void* release() { void* result = ptr; ptr = nullptr; return result; }
  inline AutoDeleter(void* ptr): ptr(ptr) {}
  inline ~AutoDeleter() { operator delete(ptr); }
};

Kenton Varda's avatar
Kenton Varda committed
71 72 73
void* HeapArrayDisposer::allocateImpl(size_t elementSize, size_t elementCount, size_t capacity,
                                      void (*constructElement)(void*),
                                      void (*destroyElement)(void*)) {
74
  AutoDeleter result(operator new(elementSize * capacity));
Kenton Varda's avatar
Kenton Varda committed
75 76 77 78

  if (constructElement == nullptr) {
    // Nothing to do.
  } else if (destroyElement == nullptr) {
79
    byte* pos = reinterpret_cast<byte*>(result.ptr);
Kenton Varda's avatar
Kenton Varda committed
80 81 82 83 84 85
    while (elementCount > 0) {
      constructElement(pos);
      pos += elementSize;
      --elementCount;
    }
  } else {
86
    ExceptionSafeArrayUtil guard(result.ptr, elementSize, 0, destroyElement);
87 88
    guard.construct(elementCount, constructElement);
    guard.release();
Kenton Varda's avatar
Kenton Varda committed
89 90
  }

91
  return result.release();
Kenton Varda's avatar
Kenton Varda committed
92 93 94 95 96 97
}

void HeapArrayDisposer::disposeImpl(
    void* firstElement, size_t elementSize, size_t elementCount, size_t capacity,
    void (*destroyElement)(void*)) const {
  // Note that capacity is ignored since operator delete() doesn't care about it.
98
  AutoDeleter deleter(firstElement);
Kenton Varda's avatar
Kenton Varda committed
99

100
  if (destroyElement != nullptr) {
101
    ExceptionSafeArrayUtil guard(firstElement, elementSize, elementCount, destroyElement);
Kenton Varda's avatar
Kenton Varda committed
102 103 104 105 106 107
    guard.destroyAll();
  }
}

const HeapArrayDisposer HeapArrayDisposer::instance = HeapArrayDisposer();

108
}  // namespace _ (private)
109
}  // namespace kj