summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/memory_manager_stateless
diff options
context:
space:
mode:
Diffstat (limited to 'ml/dlib/dlib/memory_manager_stateless')
-rw-r--r--ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_1.h86
-rw-r--r--ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_2.h119
-rw-r--r--ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_abstract.h142
3 files changed, 347 insertions, 0 deletions
diff --git a/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_1.h b/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_1.h
new file mode 100644
index 000000000..0d5794d54
--- /dev/null
+++ b/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_1.h
@@ -0,0 +1,86 @@
+// Copyright (C) 2006 Davis E. King (davis@dlib.net)
+// License: Boost Software License See LICENSE.txt for the full license.
+#ifndef DLIB_MEMORY_MANAGER_STATELESs_1_
+#define DLIB_MEMORY_MANAGER_STATELESs_1_
+
+#include "memory_manager_stateless_kernel_abstract.h"
+
+namespace dlib
+{
+ template <
+ typename T
+ >
+ class memory_manager_stateless_kernel_1
+ {
+ /*!
+ this implementation just calls new and delete directly
+ !*/
+
+ public:
+
+ typedef T type;
+ const static bool is_stateless = true;
+
+ template <typename U>
+ struct rebind {
+ typedef memory_manager_stateless_kernel_1<U> other;
+ };
+
+ memory_manager_stateless_kernel_1(
+ )
+ {}
+
+ virtual ~memory_manager_stateless_kernel_1(
+ ) {}
+
+ T* allocate (
+ )
+ {
+ return new T;
+ }
+
+ void deallocate (
+ T* item
+ )
+ {
+ delete item;
+ }
+
+ T* allocate_array (
+ unsigned long size
+ )
+ {
+ return new T[size];
+ }
+
+ void deallocate_array (
+ T* item
+ )
+ {
+ delete [] item;
+ }
+
+ void swap (memory_manager_stateless_kernel_1&)
+ {}
+
+ private:
+
+ // restricted functions
+ memory_manager_stateless_kernel_1(memory_manager_stateless_kernel_1&); // copy constructor
+ memory_manager_stateless_kernel_1& operator=(memory_manager_stateless_kernel_1&); // assignment operator
+ };
+
+ template <
+ typename T
+ >
+ inline void swap (
+ memory_manager_stateless_kernel_1<T>& a,
+ memory_manager_stateless_kernel_1<T>& b
+ ) { a.swap(b); }
+
+}
+
+#endif // DLIB_MEMORY_MANAGER_STATELESs_1_
+
+
+
diff --git a/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_2.h b/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_2.h
new file mode 100644
index 000000000..7c4bf76f5
--- /dev/null
+++ b/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_2.h
@@ -0,0 +1,119 @@
+// Copyright (C) 2006 Davis E. King (davis@dlib.net)
+// License: Boost Software License See LICENSE.txt for the full license.
+#ifndef DLIB_MEMORY_MANAGER_STATELESs_2_
+#define DLIB_MEMORY_MANAGER_STATELESs_2_
+
+#include "../algs.h"
+#include "memory_manager_stateless_kernel_abstract.h"
+#include "../threads.h"
+
+namespace dlib
+{
+ template <
+ typename T,
+ typename mem_manager
+ >
+ class memory_manager_stateless_kernel_2
+ {
+ /*!
+ REQUIREMENTS ON mem_manager
+ mem_manager must be an implementation of memory_manager/memory_manager_kernel_abstract.h
+
+ CONVENTION
+ this object has a single global instance of mem_manager
+ !*/
+
+ public:
+
+ typedef T type;
+ const static bool is_stateless = true;
+
+ template <typename U>
+ struct rebind {
+ typedef memory_manager_stateless_kernel_2<U,mem_manager> other;
+ };
+
+ memory_manager_stateless_kernel_2(
+ )
+ {
+ // call this just to make sure the mutex is is initialized before
+ // multiple threads start calling the member functions.
+ global_mutex();
+ }
+
+ virtual ~memory_manager_stateless_kernel_2(
+ ) {}
+
+ T* allocate (
+ )
+ {
+ auto_mutex M(global_mutex());
+ return global_mm().allocate();
+ }
+
+ void deallocate (
+ T* item
+ )
+ {
+ auto_mutex M(global_mutex());
+ return global_mm().deallocate(item);
+ }
+
+ T* allocate_array (
+ unsigned long size
+ )
+ {
+ auto_mutex M(global_mutex());
+ return global_mm().allocate_array(size);
+ }
+
+ void deallocate_array (
+ T* item
+ )
+ {
+ auto_mutex M(global_mutex());
+ return global_mm().deallocate_array(item);
+ }
+
+ void swap (memory_manager_stateless_kernel_2&)
+ {}
+
+ private:
+
+ static mutex& global_mutex (
+ )
+ {
+ static mutex lock;
+ return lock;
+ }
+
+ typedef typename mem_manager::template rebind<T>::other rebound_mm_type;
+
+ static rebound_mm_type& global_mm (
+ )
+ {
+ static rebound_mm_type mm;
+ return mm;
+ }
+
+ // restricted functions
+ memory_manager_stateless_kernel_2(memory_manager_stateless_kernel_2&); // copy constructor
+ memory_manager_stateless_kernel_2& operator=(memory_manager_stateless_kernel_2&); // assignment operator
+ };
+
+ template <
+ typename T,
+ typename mem_manager
+ >
+ inline void swap (
+ memory_manager_stateless_kernel_2<T,mem_manager>& a,
+ memory_manager_stateless_kernel_2<T,mem_manager>& b
+ ) { a.swap(b); }
+
+}
+
+#endif // DLIB_MEMORY_MANAGER_STATELESs_2_
+
+
+
+
diff --git a/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_abstract.h b/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_abstract.h
new file mode 100644
index 000000000..2c5b1e73c
--- /dev/null
+++ b/ml/dlib/dlib/memory_manager_stateless/memory_manager_stateless_kernel_abstract.h
@@ -0,0 +1,142 @@
+// Copyright (C) 2006 Davis E. King (davis@dlib.net)
+// License: Boost Software License See LICENSE.txt for the full license.
+#undef DLIB_MEMORY_MANAGER_STATELESs_ABSTRACT_
+#ifdef DLIB_MEMORY_MANAGER_STATELESs_ABSTRACT_
+
+#include "../algs.h"
+
+namespace dlib
+{
+ template <
+ typename T
+ >
+ class memory_manager_stateless
+ {
+ /*!
+ REQUIREMENTS ON T
+ T must have a default constructor.
+
+ WHAT THIS OBJECT REPRESENTS
+ This object represents some kind of stateless memory manager or memory pool.
+ Stateless means that all instances (instances of the same kernel implementation that is)
+ of this object are identical and can be used interchangeably. Note that
+ implementations are allowed to have some shared global state such as a
+ global memory pool.
+
+ THREAD SAFETY
+ This object is thread safe. You may access it from any thread at any time
+ without synchronizing access.
+ !*/
+
+ public:
+
+ typedef T type;
+ const static bool is_stateless = true;
+
+ template <typename U>
+ struct rebind {
+ typedef memory_manager_stateless<U> other;
+ };
+
+ memory_manager_stateless(
+ );
+ /*!
+ ensures
+ - #*this is properly initialized
+ throws
+ - std::bad_alloc
+ !*/
+
+ virtual ~memory_manager_stateless(
+ );
+ /*!
+ ensures
+ - frees any resources used by *this but has no effect on any shared global
+ resources used by the implementation.
+ !*/
+
+ T* allocate (
+ );
+ /*!
+ ensures
+ - allocates a new object of type T and returns a pointer to it.
+ throws
+ - std::bad_alloc or any exception thrown by T's constructor.
+ If this exception is thrown then the call to allocate()
+ has no effect on #*this.
+ !*/
+
+ void deallocate (
+ T* item
+ );
+ /*!
+ requires
+ - item == is a pointer to memory that was obtained from a call to
+ allocate(). (i.e. The pointer you are deallocating must have
+ come from the same implementation of memory_manager_stateless
+ that is trying to deallocate it.)
+ - the memory pointed to by item hasn't already been deallocated.
+ ensures
+ - deallocates the object pointed to by item
+ !*/
+
+ T* allocate_array (
+ unsigned long size
+ );
+ /*!
+ ensures
+ - allocates a new array of size objects of type T and returns a
+ pointer to it.
+ throws
+ - std::bad_alloc or any exception thrown by T's constructor.
+ If this exception is thrown then the call to allocate()
+ has no effect on #*this.
+ !*/
+
+ void deallocate_array (
+ T* item
+ );
+ /*!
+ requires
+ - item == is a pointer to memory that was obtained from a call to
+ allocate_array(). (i.e. The pointer you are deallocating must have
+ come from the same implementation of memory_manager_stateless
+ that is trying to deallocate it.)
+ - the memory pointed to by item hasn't already been deallocated.
+ ensures
+ - deallocates the array pointed to by item
+ !*/
+
+ void swap (
+ memory_manager_stateless& item
+ );
+ /*!
+ ensures
+ - this function has no effect on *this or item. It is just provided
+ to make this object's interface more compatable with the other
+ memory managers.
+ !*/
+
+ private:
+
+ // restricted functions
+ memory_manager_stateless(memory_manager_stateless&); // copy constructor
+ memory_manager_stateless& operator=(memory_manager_stateless&); // assignment operator
+ };
+
+ template <
+ typename T
+ >
+ inline void swap (
+ memory_manager_stateless<T>& a,
+ memory_manager_stateless<T>& b
+ ) { a.swap(b); }
+ /*!
+ provides a global swap function
+ !*/
+
+}
+
+#endif // DLIB_MEMORY_MANAGER_STATELESs_ABSTRACT_
+
+