Kea: memory_segment.h Source File

| Kea 3.0.0

memory_segment.h
Go to the documentation of this file.
1// Copyright (C) 2012-2024 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#ifndef MEMORY_SEGMENT_H
8#define MEMORY_SEGMENT_H
9
10#include <exceptions/exceptions.h>
11
12#include <utility>
13
14#include <stdlib.h>
15
16namespace isc {
17namespace util {
18
21 class MemorySegmentOpenError : public Exception {
22public:
23 MemorySegmentOpenError(const char* file, size_t line, const char* what) :
24 isc::Exception(file, line, what) {}
25};
26
31 class MemorySegmentGrown : public Exception {
32public:
33 MemorySegmentGrown(const char* file, size_t line, const char* what) :
34 isc::Exception(file, line, what) {}
35};
36
39 class MemorySegmentError : public Exception {
40public:
41 MemorySegmentError(const char* file, size_t line, const char* what) :
42 isc::Exception(file, line, what) {}
43};
44
54 class MemorySegment {
55public:
57 virtual ~MemorySegment() {}
58
118 virtual void* allocate(size_t size) = 0;
119
145 virtual void deallocate(void* ptr, size_t size) = 0;
146
152 virtual bool allMemoryDeallocated() const = 0;
153
222 bool setNamedAddress(const char* name, void* addr) {
223 // This public method implements common validation. The actual
224 // work specific to the derived segment is delegated to the
225 // corresponding protected method.
226 validateName(name);
227 return (setNamedAddressImpl(name, addr));
228 }
229
231 typedef std::pair<bool, void*> NamedAddressResult;
232
258 NamedAddressResult getNamedAddress(const char* name) const {
259 // This public method implements common validation. The actual
260 // work specific to the derived segment is delegated to the
261 // corresponding protected method.
262 validateName(name);
263 return (getNamedAddressImpl(name));
264 }
265
286 bool clearNamedAddress(const char* name) {
287 // This public method implements common validation. The actual
288 // work specific to the derived segment is delegated to the
289 // corresponding protected method.
290 validateName(name);
291 return (clearNamedAddressImpl(name));
292 }
293
294private:
303 static void validateName(const char* name) {
304 if (!name) {
305 isc_throw(InvalidParameter, "NULL is invalid for a name.");
306 } else if (*name == '\0') {
307 isc_throw(InvalidParameter, "Empty names are invalid.");
308 } else if (*name == '_') {
309 isc_throw(InvalidParameter,
310 "Names beginning with '_' are reserved for "
311 "internal use only.");
312 }
313 }
314
315protected:
317 virtual bool setNamedAddressImpl(const char* name, void* addr) = 0;
318
320 virtual NamedAddressResult getNamedAddressImpl(const char* name) const = 0;
321
323 virtual bool clearNamedAddressImpl(const char* name) = 0;
324};
325
326} // namespace util
327} // namespace isc
328
329#endif // MEMORY_SEGMENT_H
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::InvalidParameter
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
Definition exceptions/exceptions.h:124
isc::util::MemorySegmentError::MemorySegmentError
MemorySegmentError(const char *file, size_t line, const char *what)
Definition memory_segment.h:41
isc::util::MemorySegmentGrown::MemorySegmentGrown
MemorySegmentGrown(const char *file, size_t line, const char *what)
Definition memory_segment.h:33
isc::util::MemorySegmentOpenError::MemorySegmentOpenError
MemorySegmentOpenError(const char *file, size_t line, const char *what)
Definition memory_segment.h:23
isc::util::MemorySegment
Memory Segment Class.
Definition memory_segment.h:54
isc::util::MemorySegment::~MemorySegment
virtual ~MemorySegment()
Destructor.
Definition memory_segment.h:57
isc::util::MemorySegment::clearNamedAddress
bool clearNamedAddress(const char *name)
Delete a name previously associated with a segment address.
Definition memory_segment.h:286
isc::util::MemorySegment::deallocate
virtual void deallocate(void *ptr, size_t size)=0
Free/release a segment of memory.
isc::util::MemorySegment::setNamedAddressImpl
virtual bool setNamedAddressImpl(const char *name, void *addr)=0
Implementation of setNamedAddress beyond common validation.
isc::util::MemorySegment::clearNamedAddressImpl
virtual bool clearNamedAddressImpl(const char *name)=0
Implementation of clearNamedAddress beyond common validation.
isc::util::MemorySegment::allMemoryDeallocated
virtual bool allMemoryDeallocated() const =0
Check if all allocated memory was deallocated.
isc::util::MemorySegment::getNamedAddress
NamedAddressResult getNamedAddress(const char *name) const
Return the address in the segment that has the given name.
Definition memory_segment.h:258
isc::util::MemorySegment::NamedAddressResult
std::pair< bool, void * > NamedAddressResult
Type definition for result returned by getNamedAddress()
Definition memory_segment.h:231
isc::util::MemorySegment::setNamedAddress
bool setNamedAddress(const char *name, void *addr)
Associate specified address in the segment with a given name.
Definition memory_segment.h:222
isc::util::MemorySegment::allocate
virtual void * allocate(size_t size)=0
Allocate/acquire a fragment of memory.
isc::util::MemorySegment::getNamedAddressImpl
virtual NamedAddressResult getNamedAddressImpl(const char *name) const =0
Implementation of getNamedAddress beyond common validation.
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::util::file
Definition filesystem.cc:28
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148