1
# Buffers allocated on the heap
3
from bgenOutput import *
4
from bgenType import OutputOnlyMixIn
5
from bgenBuffer import FixedInputOutputBufferType
8
class HeapInputOutputBufferType(FixedInputOutputBufferType):
10
"""Input-output buffer allocated on the heap -- passed as (inbuffer, outbuffer, size).
12
Instantiate without parameters.
13
Call from Python with input buffer.
16
def __init__(self, datatype = 'char', sizetype = 'int', sizeformat = None):
17
FixedInputOutputBufferType.__init__(self, "0", datatype, sizetype, sizeformat)
19
def getOutputBufferDeclarations(self, name, constmode=False, outmode=False):
21
raise RuntimeError, "Cannot use const output buffer"
26
return ["%s%s *%s__out__" % (self.datatype, out, name)]
28
def getargsCheck(self, name):
29
Output("if ((%s__out__ = malloc(%s__in_len__)) == NULL)", name, name)
31
Output('PyErr_NoMemory();')
32
Output("goto %s__error__;", name)
35
Output("%s__len__ = %s__in_len__;", name, name)
37
def passOutput(self, name):
38
return "%s__in__, %s__out__, (%s)%s__len__" % \
39
(name, name, self.sizetype, name)
41
def mkvalueArgs(self, name):
42
return "%s__out__, (int)%s__len__" % (name, name)
44
def cleanup(self, name):
45
Output("free(%s__out__);", name)
46
FixedInputOutputBufferType.cleanup(self, name)
49
class VarHeapInputOutputBufferType(HeapInputOutputBufferType):
51
"""same as base class, but passed as (inbuffer, outbuffer, &size)"""
53
def passOutput(self, name):
54
return "%s__in__, %s__out__, &%s__len__" % (name, name, name)
57
class HeapCombinedInputOutputBufferType(HeapInputOutputBufferType):
59
"""same as base class, but passed as (inoutbuffer, size)"""
61
def passOutput(self, name):
62
return "(%s *)memcpy(%s__out__, %s__in__, %s__len__)" % \
63
(self.datatype, name, name, name)
66
class VarHeapCombinedInputOutputBufferType(HeapInputOutputBufferType):
68
"""same as base class, but passed as (inoutbuffer, &size)"""
70
def passOutput(self, name):
71
return "(%s *)memcpy(%s__out__, %s__in__, &%s__len__)" % \
72
(self.datatype, name, name, name)
75
class HeapOutputBufferType(OutputOnlyMixIn, HeapInputOutputBufferType):
77
"""Output buffer allocated on the heap -- passed as (buffer, size).
79
Instantiate without parameters.
80
Call from Python with buffer size.
83
def getInputBufferDeclarations(self, name, constmode=False):
86
def getargsFormat(self):
89
def getargsArgs(self, name):
90
return "&%s__in_len__" % name
92
def passOutput(self, name):
93
return "%s__out__, %s__len__" % (name, name)
96
class VarHeapOutputBufferType(HeapOutputBufferType):
98
"""Output buffer allocated on the heap -- passed as (buffer, &size).
100
Instantiate without parameters.
101
Call from Python with buffer size.
104
def passOutput(self, name):
105
return "%s__out__, &%s__len__" % (name, name)
108
class VarVarHeapOutputBufferType(VarHeapOutputBufferType):
110
"""Output buffer allocated on the heap -- passed as (buffer, size, &size).
112
Instantiate without parameters.
113
Call from Python with buffer size.
116
def passOutput(self, name):
117
return "%s__out__, %s__len__, &%s__len__" % (name, name, name)
119
class MallocHeapOutputBufferType(HeapOutputBufferType):
120
"""Output buffer allocated by the called function -- passed as (&buffer, &size).
122
Instantiate without parameters.
123
Call from Python without parameters.
126
def getargsCheck(self, name):
127
Output("%s__out__ = NULL;", name)
129
def getAuxDeclarations(self, name):
132
def passOutput(self, name):
133
return "&%s__out__, &%s__len__" % (name, name)
135
def getargsFormat(self):
138
def getargsArgs(self, name):
141
def mkvalueFormat(self):
144
def cleanup(self, name):
145
Output("if( %s__out__ ) free(%s__out__);", name, name)