blob: c6160ba479438d89a862906aa1691beec1ac8383 [file] [log] [blame]
Guido van Rossum01f5a811995-01-25 22:59:21 +00001# Buffers allocated on the heap
2
3from bgenOutput import *
4from bgenType import OutputOnlyMixIn
5from bgenBuffer import FixedInputOutputBufferType
6
7
8class HeapInputOutputBufferType(FixedInputOutputBufferType):
9
10 """Input-output buffer allocated on the heap -- passed as (inbuffer, outbuffer, size).
11
12 Instantiate without parameters.
13 Call from Python with input buffer.
14 """
15
16 def __init__(self, datatype = 'char', sizetype = 'int', sizeformat = None):
17 FixedInputOutputBufferType.__init__(self, "0", datatype, sizetype, sizeformat)
18
19 def declareOutputBuffer(self, name):
20 Output("%s *%s__out__;", self.datatype, name)
21
22 def getargsCheck(self, name):
Guido van Rossum227a4231995-03-10 14:42:57 +000023 Output("if ((%s__out__ = malloc(%s__in_len__)) == NULL)", name, name)
Guido van Rossum01f5a811995-01-25 22:59:21 +000024 OutLbrace()
25 Output('PyErr_NoMemory();')
26 Output("goto %s__error__;", name)
27 OutRbrace()
Guido van Rossum227a4231995-03-10 14:42:57 +000028 Output("%s__len__ = %s__in_len__;", name, name)
Guido van Rossum01f5a811995-01-25 22:59:21 +000029
30 def passOutput(self, name):
Guido van Rossum227a4231995-03-10 14:42:57 +000031 return "%s__in__, %s__out__, (%s)%s__len__" % \
32 (name, name, self.sizetype, name)
Guido van Rossum01f5a811995-01-25 22:59:21 +000033
34 def mkvalueArgs(self, name):
Guido van Rossum227a4231995-03-10 14:42:57 +000035 return "%s__out__, (int)%s__len__" % (name, name)
Guido van Rossum01f5a811995-01-25 22:59:21 +000036
37 def cleanup(self, name):
38 Output("free(%s__out__);", name)
39 FixedInputOutputBufferType.cleanup(self, name)
40
41
42class VarHeapInputOutputBufferType(HeapInputOutputBufferType):
43
44 """same as base class, but passed as (inbuffer, outbuffer, &size)"""
45
46 def passOutput(self, name):
47 return "%s__in__, %s__out__, &%s__len__" % (name, name, name)
48
49
50class HeapCombinedInputOutputBufferType(HeapInputOutputBufferType):
51
52 """same as base class, but passed as (inoutbuffer, size)"""
53
54 def passOutput(self, name):
55 return "(%s *)memcpy(%s__out__, %s__in__, %s__len__)" % \
56 (self.datatype, name, name, name)
57
58
59class VarHeapCombinedInputOutputBufferType(HeapInputOutputBufferType):
60
61 """same as base class, but passed as (inoutbuffer, &size)"""
62
63 def passOutput(self, name):
64 return "(%s *)memcpy(%s__out__, %s__in__, &%s__len__)" % \
65 (self.datatype, name, name, name)
66
67
68class HeapOutputBufferType(OutputOnlyMixIn, HeapInputOutputBufferType):
69
70 """Output buffer allocated on the heap -- passed as (buffer, size).
71
72 Instantiate without parameters.
73 Call from Python with buffer size.
74 """
75
76 def declareInputBuffer(self, name):
77 pass
78
79 def getargsFormat(self):
Guido van Rossum227a4231995-03-10 14:42:57 +000080 return "i"
Guido van Rossum01f5a811995-01-25 22:59:21 +000081
82 def getargsArgs(self, name):
Guido van Rossum227a4231995-03-10 14:42:57 +000083 return "&%s__in_len__" % name
Guido van Rossum01f5a811995-01-25 22:59:21 +000084
85 def passOutput(self, name):
86 return "%s__out__, %s__len__" % (name, name)
87
88
89class VarHeapOutputBufferType(HeapOutputBufferType):
90
91 """Output buffer allocated on the heap -- passed as (buffer, &size).
92
93 Instantiate without parameters.
94 Call from Python with buffer size.
95 """
96
97 def passOutput(self, name):
98 return "%s__out__, &%s__len__" % (name, name)
99
100
101class VarVarHeapOutputBufferType(VarHeapOutputBufferType):
102
103 """Output buffer allocated on the heap -- passed as (buffer, size, &size).
104
105 Instantiate without parameters.
106 Call from Python with buffer size.
107 """
108
109 def passOutput(self, name):
110 return "%s__out__, %s__len__, &%s__len__" % (name, name, name)