OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CpuMgrArenaAllocator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "CpuMgrArenaAllocator.h"
18 
19 #include "Catalog/SysCatalog.h"
20 
21 namespace {
23  const auto& sys_catalog = Catalog_Namespace::SysCatalog::instance();
24  CHECK(sys_catalog.isInitialized());
25  return sys_catalog.getDataMgr();
26 }
27 } // namespace
28 
30  : data_mgr_(get_data_mgr_instance()), size_(0) {}
31 
33  for (auto buffer : allocated_buffers_) {
34  data_mgr_.free(buffer);
35  }
36 }
37 
38 void* CpuMgrArenaAllocator::allocate(size_t num_bytes) {
39  if (num_bytes == 0) {
40  return nullptr;
41  }
42  AbstractBuffer* buffer = nullptr;
43  try {
44  buffer = data_mgr_.alloc(Data_Namespace::CPU_LEVEL, 0, num_bytes);
45  } catch (const OutOfMemory& e) {
46  LOG(ERROR) << e.what();
47  throw OutOfHostMemory(num_bytes);
48  }
49  CHECK(buffer);
50  allocated_buffers_.emplace_back(buffer);
51 
52  auto mem_ptr = buffer->getMemoryPtr();
53  CHECK(mem_ptr);
54  size_ += num_bytes;
55  return mem_ptr;
56 }
57 
58 void* CpuMgrArenaAllocator::allocateAndZero(const size_t num_bytes) {
59  auto ret = allocate(num_bytes);
60  std::memset(ret, 0, num_bytes);
61  return ret;
62 }
63 
65  return size_;
66 }
67 
69  return size_;
70 }
71 
74 }
size_t totalBytes() const override
void * allocate(size_t num_bytes) override
#define LOG(tag)
Definition: Logger.h:285
virtual int8_t * getMemoryPtr()=0
static SysCatalog & instance()
Definition: SysCatalog.h:343
This file contains the class specification and related data structures for SysCatalog.
An AbstractBuffer is a unit of data management for a data manager.
Allocate CPU memory using CpuBuffers via DataMgr.
MemoryType getMemoryType() const override
std::vector< Data_Namespace::AbstractBuffer * > allocated_buffers_
data_mgr_(data_mgr)
Data_Namespace::DataMgr & data_mgr_
#define CHECK(condition)
Definition: Logger.h:291
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:614
size_t bytesUsed() const override
void * allocateAndZero(const size_t num_bytes) override
AbstractBuffer * alloc(const MemoryLevel memoryLevel, const int deviceId, const size_t numBytes)
Definition: DataMgr.cpp:605