github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/allocator.cc (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see 17 // <http://www.gnu.org/licenses/>. 18 // 19 20 #include "allocator.h" 21 #include <stdio.h> 22 #include <stdlib.h> 23 24 ArrayBufferAllocator::ArrayBufferAllocator() 25 : total_allocated_size_(0L), peak_allocated_size_(0L) {} 26 27 ArrayBufferAllocator::~ArrayBufferAllocator() {} 28 29 /** 30 * Allocate |length| bytes. Return NULL if allocation is not successful. 31 * Memory should be initialized to zeroes. 32 */ 33 void *ArrayBufferAllocator::Allocate(size_t length) { 34 this->total_allocated_size_ += length; 35 if (this->total_allocated_size_ > this->peak_allocated_size_) { 36 this->peak_allocated_size_ = this->total_allocated_size_; 37 } 38 return calloc(length, 1); 39 } 40 41 /** 42 * Allocate |length| bytes. Return NULL if allocation is not successful. 43 * Memory does not have to be initialized. 44 */ 45 void *ArrayBufferAllocator::AllocateUninitialized(size_t length) { 46 this->total_allocated_size_ += length; 47 if (this->total_allocated_size_ > this->peak_allocated_size_) { 48 this->peak_allocated_size_ = this->total_allocated_size_; 49 } 50 return malloc(length); 51 } 52 53 /** 54 * Free the memory block of size |length|, pointed to by |data|. 55 * That memory is guaranteed to be previously allocated by |Allocate|. 56 */ 57 void ArrayBufferAllocator::Free(void *data, size_t length) { 58 this->total_allocated_size_ -= length; 59 free(data); 60 } 61 62 size_t ArrayBufferAllocator::total_available_size() { 63 return this->total_allocated_size_; 64 } 65 66 size_t ArrayBufferAllocator::peak_allocated_size() { 67 return this->peak_allocated_size_; 68 }