1 #ifndef INCLUDED_NJN_MEMUTIL 2 #define INCLUDED_NJN_MEMUTIL 3 4 /* $Id: $ 5 * =========================================================================== 6 * 7 * PUBLIC DOMAIN NOTICE 8 * National Center for Biotechnology Information 9 * 10 * This software/database is a "United States Government Work" under the 11 * terms of the United States Copyright Act. It was written as part of 12 * the author's offical duties as a United States Government employee and 13 * thus cannot be copyrighted. This software/database is freely available 14 * to the public for use. The National Library of Medicine and the U.S. 15 * Government have not placed any restriction on its use or reproduction. 16 * 17 * Although all reasonable efforts have been taken to ensure the accuracy 18 * and reliability of the software and data, the NLM and the U.S. 19 * Government do not and cannot warrant the performance or results that 20 * may be obtained by using this software or data. The NLM and the U.S. 21 * Government disclaim all warranties, express or implied, including 22 * warranties of performance, merchantability or fitness for any particular 23 * purpose. 24 * 25 * Please cite the author in any work or product based on this material. 26 * 27 * ===========================================================================*/ 28 29 /***************************************************************************** 30 31 File name: njn_memutil.hpp 32 33 Author: John Spouge 34 35 Contents: 36 37 ******************************************************************************/ 38 39 #include "njn_ioutil.hpp" 40 41 42 namespace Njn { 43 namespace MemUtil { 44 45 inline void *memNew (size_t size); 46 47 inline void *memMore (void *p, size_t size); 48 49 inline void memCpy (void *to, const void *from, size_t size); 50 51 inline void *memSet (void *p, int c, size_t size); 52 53 inline void *memZero (void *p, size_t size); 54 55 template <typename T> 56 T **newMatrix (size_t m_, size_t n_); // matrix [0...m_)[0...n_) 57 58 template <typename T> 59 void deleteMatrix (T **&matrix, size_t m_, size_t n_); 60 // Assumes matrix was created with the call 61 // matrix = newMatrix <T> (m_, n_); 62 63 // 64 // There are no more declarations beyond this point. 65 // 66 67 memNew(size_t size)68 inline void *memNew (size_t size) 69 { 70 return (void *) (size == 0 ? 0 : new char [size]); 71 } 72 memMore(void * p,size_t size)73 inline void *memMore (void *p, size_t size) 74 { 75 return (void *) (p == 0 ? new char [size] : realloc (p, size)); 76 } 77 memCpy(void * to,const void * from,size_t size)78 inline void memCpy (void *to, const void *from, size_t size) 79 { 80 if (size != 0) memcpy (to, from, size); 81 } 82 memSet(void * p,int c,size_t size)83 inline void *memSet (void *p, int c, size_t size) 84 { 85 return memset (p, c, size); 86 } 87 memZero(void * p,size_t size)88 inline void *memZero (void *p, size_t size) 89 { 90 return memset (p, '\0', size); 91 } 92 93 template <typename T> newMatrix(size_t m_,size_t n_)94 T **newMatrix (size_t m_, size_t n_) // matrix [iBegin...iEnd)[jBegin...jEnd) 95 { 96 T **matrix = new T * [m_]; 97 98 for (size_t index = 0; index != m_; index++) 99 { 100 matrix [index] = new T [n_]; 101 } 102 103 return matrix; 104 } 105 106 template <typename T> 107 deleteMatrix(T ** & matrix,size_t m_,size_t)108 void deleteMatrix (T **&matrix, size_t m_ , size_t /*sls deleted n_*/) 109 // Assumes matrix was created with the call 110 // matrix = newMatrix <T> (iBegin, iEnd, jBegin, jEnd); 111 { 112 for (size_t index = 0; index != m_; index++) 113 { 114 delete [] matrix [index]; 115 } 116 117 delete [] matrix; 118 matrix = 0; 119 } 120 121 } 122 } 123 124 125 126 #endif 127 128