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

Last change on this file since 438 was 438, checked in by kulshres, 6 years ago

Initialize SubBufferElements? to zero by default

SubBufferElements? may not be initialized with zeros automatically as many
recent systems do not initialize allocated memory automatically to zero.
Since we check later for zero we must initialize them in the default case
to be zero.

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

  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     struct_buf.h
4 Revision: $Id: buffer_temp.h 438 2013-10-09 10:43:18Z 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
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/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, FunctionPointer, _subBufferSize>
30#define BUFFER_TEMPLATE template<class SubBufferElement,\
31   class FunctionPointer, IndexType _subBufferSize>
32
33typedef locint IndexType;
34
35BUFFER_TEMPLATE class Buffer {
36
37    typedef void (*InitFunctionPointer) (SubBufferElement *subBufferElement);
38
39    static void zeroAll(SubBufferElement* subBufferElement);
40
41    typedef struct SubBuffer {
42        SubBufferElement elements[_subBufferSize];
43        struct SubBuffer *nextSubBuffer;
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(FunctionPointer functionPointer);
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::~Buffer() {
82    SubBuffer *tmpSubBuffer = NULL;
83
84    while (firstSubBuffer != NULL) {
85        tmpSubBuffer = firstSubBuffer;
86        firstSubBuffer = firstSubBuffer->nextSubBuffer;
87        delete tmpSubBuffer;
88    }
89}
90
91BUFFER_TEMPLATE
92SubBufferElement *BUFFER::append(FunctionPointer functionPointer) {
93    SubBuffer *currentSubBuffer=firstSubBuffer, *previousSubBuffer=NULL;
94    IndexType index, tmp=numEntries;
95
96    if (functionPointer==NULL) fail(ADOLC_BUFFER_NULLPOINTER_FUNCTION);
97
98    while (tmp>=subBufferSize) {
99        previousSubBuffer=currentSubBuffer;
100        currentSubBuffer=currentSubBuffer->nextSubBuffer;
101        tmp-=subBufferSize;
102    }
103    if (currentSubBuffer==NULL) {
104        currentSubBuffer=new SubBuffer;
105        if (firstSubBuffer==NULL) firstSubBuffer=currentSubBuffer;
106        else previousSubBuffer->nextSubBuffer=currentSubBuffer;
107        currentSubBuffer->nextSubBuffer=NULL;
108    }
109    index=tmp;
110
111    if (initFunction!=NULL)
112        initFunction(&(currentSubBuffer->elements[index]));
113
114    currentSubBuffer->elements[index].index=numEntries;
115    currentSubBuffer->elements[index].function=functionPointer;
116    ++numEntries;
117
118    return &currentSubBuffer->elements[index];
119}
120
121BUFFER_TEMPLATE
122SubBufferElement *BUFFER::getElement(IndexType index) {
123    SubBuffer *currentSubBuffer=firstSubBuffer;
124
125    if (index>=numEntries) fail(ADOLC_BUFFER_INDEX_TO_LARGE);
126    while (index>=subBufferSize) {
127        currentSubBuffer=currentSubBuffer->nextSubBuffer;
128        index-=subBufferSize;
129    }
130    return &currentSubBuffer->elements[index];
131}
132
133#endif /* __cplusplus */
134
135#endif /* ADOLC_STRUCT_BUF_H */
136
Note: See TracBrowser for help on using the repository browser.