Kea: memory_segment_local.cc Source File

| Kea 3.2.0-git

memory_segment_local.cc
Go to the documentation of this file.
1// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <util/memory_segment_local.h>
10#include <exceptions/exceptions.h>
11
12namespace isc {
13namespace util {
14
15void*
16MemorySegmentLocal::allocate(size_t size) {
17 void* ptr = malloc(size);
18 if (ptr == NULL) {
19 throw std::bad_alloc();
20 }
21
22 allocated_size_ += size;
23 return (ptr);
24}
25
26void
27MemorySegmentLocal::deallocate(void* ptr, size_t size) {
28 if (ptr == NULL) {
29 // Return early if NULL is passed to be deallocated (without
30 // modifying allocated_size, or comparing against it).
31 return;
32 }
33
34 if (size > allocated_size_) {
35 isc_throw(OutOfRange, "Invalid size to deallocate: " << size
36 << "; currently allocated size: " << allocated_size_);
37 }
38
39 allocated_size_ -= size;
40 free(ptr);
41}
42
43bool
44MemorySegmentLocal::allMemoryDeallocated() const {
45 return (allocated_size_ == 0 && named_addrs_.empty());
46}
47
48 MemorySegment::NamedAddressResult
49MemorySegmentLocal::getNamedAddressImpl(const char* name) const {
50 std::map<std::string, void*>::const_iterator found =
51 named_addrs_.find(name);
52 if (found != named_addrs_.end()) {
53 return (NamedAddressResult(true, found->second));
54 }
55 return (NamedAddressResult(false, static_cast<void*>(0)));
56}
57
58bool
59MemorySegmentLocal::setNamedAddressImpl(const char* name, void* addr) {
60 named_addrs_[name] = addr;
61 return (false);
62}
63
64bool
65MemorySegmentLocal::clearNamedAddressImpl(const char* name) {
66 const size_t n_erased = named_addrs_.erase(name);
67 return (n_erased != 0);
68}
69
70} // namespace util
71} // namespace isc
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition exceptions/exceptions.h:115
isc::util::MemorySegmentLocal::setNamedAddressImpl
virtual bool setNamedAddressImpl(const char *name, void *addr)
Local segment version of setNamedAddress.
Definition memory_segment_local.cc:59
isc::util::MemorySegmentLocal::clearNamedAddressImpl
virtual bool clearNamedAddressImpl(const char *name)
Local segment version of clearNamedAddress.
Definition memory_segment_local.cc:65
isc::util::MemorySegmentLocal::allocate
virtual void * allocate(size_t size)
Allocate/acquire a segment of memory.
Definition memory_segment_local.cc:16
isc::util::MemorySegmentLocal::allMemoryDeallocated
virtual bool allMemoryDeallocated() const
Check if all allocated memory was deallocated.
Definition memory_segment_local.cc:44
isc::util::MemorySegmentLocal::getNamedAddressImpl
virtual NamedAddressResult getNamedAddressImpl(const char *name) const
Local segment version of getNamedAddress.
Definition memory_segment_local.cc:49
isc::util::MemorySegmentLocal::deallocate
virtual void deallocate(void *ptr, size_t size)
Free/release a segment of memory.
Definition memory_segment_local.cc:27
isc::util::MemorySegment::NamedAddressResult
std::pair< bool, void * > NamedAddressResult
Type definition for result returned by getNamedAddress().
Definition memory_segment.h:231
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
memory_segment_local.h
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18