Kea 2.5.8
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
11
12namespace isc {
13namespace util {
14
15void*
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
45 return (allocated_size_ == 0 && named_addrs_.empty());
46}
47
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
66 const size_t n_erased = named_addrs_.erase(name);
67 return (n_erased != 0);
68}
69
70} // namespace util
71} // namespace isc
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
virtual bool setNamedAddressImpl(const char *name, void *addr)
Local segment version of setNamedAddress.
virtual bool clearNamedAddressImpl(const char *name)
Local segment version of clearNamedAddress.
virtual void * allocate(size_t size)
Allocate/acquire a segment of memory.
virtual bool allMemoryDeallocated() const
Check if all allocated memory was deallocated.
virtual NamedAddressResult getNamedAddressImpl(const char *name) const
Local segment version of getNamedAddress.
virtual void deallocate(void *ptr, size_t size)
Free/release a segment of memory.
std::pair< bool, void * > NamedAddressResult
Type definition for result returned by getNamedAddress()
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.