source: trunk/ADOL-C/src/buffer_temp.h @ 708

Last change on this file since 708 was 607, checked in by kulshres, 4 years ago

fix a memory leak in the buffer destructor

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     struct_buf.h
4 Revision: $Id: buffer_temp.h 607 2015-05-19 09:36:56Z kulshres $
5 Contents: - template class for linked list of Type buffers with constant length
6             per buffer
7           - intended to be used with structs
8 
9 Copyright (c) Andreas Kowarz, Kshitij Kulshreshtha, Jean Utke
10 
11 This file is part of ADOL-C. This software is provided as open source.
12 Any use, reproduction, or distribution of the software constitutes
13 recipient's acceptance of the terms of the accompanying license file.
14   
15----------------------------------------------------------------------------*/
16
17#if !defined(ADOLC_STRUCT_BUF_H)
18#define ADOLC_STRUCT_BUF_H 1
19
20#include <adolc/internal/common.h>
21#include "taping_p.h"
22
23#if defined(__cplusplus)
24/****************************************************************************/
25/*                                                          This is all C++ */
26
27#include <cstdlib>
28
29#define BUFFER Buffer<SubBufferElement, _subBufferSize>
30#define BUFFER_TEMPLATE template<class SubBufferElement, IndexType _subBufferSize>
31
32typedef locint IndexType;
33
34BUFFER_TEMPLATE class Buffer {
35
36    typedef void (*InitFunctionPointer) (SubBufferElement *subBufferElement);
37
38    static void zeroAll(SubBufferElement* subBufferElement);
39
40    typedef struct SubBuffer {
41        SubBufferElement elements[_subBufferSize];
42        struct SubBuffer *nextSubBuffer;
43        SubBuffer();
44    }
45    SubBuffer;
46
47public:
48    inline Buffer() {
49        firstSubBuffer = NULL;
50        numEntries = 0;
51        subBufferSize = _subBufferSize;
52        initFunction = zeroAll;
53    }
54    inline Buffer(InitFunctionPointer _initFunction) {
55        firstSubBuffer = NULL;
56        numEntries = 0;
57        subBufferSize = _subBufferSize;
58        initFunction = _initFunction;
59    }
60    inline ~Buffer();
61
62    inline void init(InitFunctionPointer _initFunction) {
63        initFunction = _initFunction;
64    }
65    SubBufferElement *append();
66    SubBufferElement *getElement(IndexType index);
67
68private:
69    SubBuffer *firstSubBuffer;
70    InitFunctionPointer initFunction;
71    IndexType subBufferSize;
72    IndexType numEntries;
73};
74
75BUFFER_TEMPLATE
76void BUFFER::zeroAll(SubBufferElement* subBufferElement) {
77    memset(subBufferElement,0,sizeof(*subBufferElement));
78}
79
80BUFFER_TEMPLATE
81BUFFER::SubBuffer::SubBuffer() {
82   memset(elements,0,sizeof(SubBufferElement)*_subBufferSize);
83   nextSubBuffer = NULL;
84}
85
86BUFFER_TEMPLATE
87BUFFER::~Buffer() {
88    SubBuffer *tmpSubBuffer = NULL;
89
90    while (firstSubBuffer != NULL) {
91        tmpSubBuffer = firstSubBuffer;
92        firstSubBuffer = firstSubBuffer->nextSubBuffer;
93        for(int i = 0; i < subBufferSize; i++)
94            if (tmpSubBuffer->elements[i].allmem != NULL)
95                free(tmpSubBuffer->elements[i].allmem);
96        delete tmpSubBuffer;
97    }
98}
99
100BUFFER_TEMPLATE
101SubBufferElement *BUFFER::append() {
102    SubBuffer *currentSubBuffer=firstSubBuffer, *previousSubBuffer=NULL;
103    IndexType index, tmp=numEntries;
104
105    while (tmp>=subBufferSize) {
106        previousSubBuffer=currentSubBuffer;
107        currentSubBuffer=currentSubBuffer->nextSubBuffer;
108        tmp-=subBufferSize;
109    }
110    if (currentSubBuffer==NULL) {
111        currentSubBuffer=new SubBuffer;
112        if (firstSubBuffer==NULL) firstSubBuffer=currentSubBuffer;
113        else previousSubBuffer->nextSubBuffer=currentSubBuffer;
114        currentSubBuffer->nextSubBuffer=NULL;
115    }
116    index=tmp;
117
118    currentSubBuffer->elements[index].allmem=NULL;
119    if (initFunction!=NULL)
120        initFunction(&(currentSubBuffer->elements[index]));
121
122    currentSubBuffer->elements[index].index=numEntries;
123    ++numEntries;
124
125    return &currentSubBuffer->elements[index];
126}
127
128BUFFER_TEMPLATE
129SubBufferElement *BUFFER::getElement(IndexType index) {
130    SubBuffer *currentSubBuffer=firstSubBuffer;
131
132    if (index>=numEntries) fail(ADOLC_BUFFER_INDEX_TO_LARGE);
133    while (index>=subBufferSize) {
134        currentSubBuffer=currentSubBuffer->nextSubBuffer;
135        index-=subBufferSize;
136    }
137    return &currentSubBuffer->elements[index];
138}
139
140#endif /* __cplusplus */
141
142#endif /* ADOLC_STRUCT_BUF_H */
143
Note: See TracBrowser for help on using the repository browser.