Guido van Rossum | 01f5a81 | 1995-01-25 22:59:21 +0000 | [diff] [blame^] | 1 | # Buffers allocated on the heap |
| 2 | |
| 3 | from bgenOutput import * |
| 4 | from bgenType import OutputOnlyMixIn |
| 5 | from bgenBuffer import FixedInputOutputBufferType |
| 6 | |
| 7 | |
| 8 | class 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): |
| 23 | Output("if ((%s__out__ = malloc(%s__len__)) == NULL)", name, name) |
| 24 | OutLbrace() |
| 25 | Output('PyErr_NoMemory();') |
| 26 | Output("goto %s__error__;", name) |
| 27 | OutRbrace() |
| 28 | |
| 29 | def passOutput(self, name): |
| 30 | return "%s__in__, %s__out__, %s__len__" % (name, name, name) |
| 31 | |
| 32 | def mkvalueArgs(self, name): |
| 33 | return "%s__out__, %s__len__" % (name, name) |
| 34 | |
| 35 | def cleanup(self, name): |
| 36 | Output("free(%s__out__);", name) |
| 37 | FixedInputOutputBufferType.cleanup(self, name) |
| 38 | |
| 39 | |
| 40 | class VarHeapInputOutputBufferType(HeapInputOutputBufferType): |
| 41 | |
| 42 | """same as base class, but passed as (inbuffer, outbuffer, &size)""" |
| 43 | |
| 44 | def passOutput(self, name): |
| 45 | return "%s__in__, %s__out__, &%s__len__" % (name, name, name) |
| 46 | |
| 47 | |
| 48 | class HeapCombinedInputOutputBufferType(HeapInputOutputBufferType): |
| 49 | |
| 50 | """same as base class, but passed as (inoutbuffer, size)""" |
| 51 | |
| 52 | def passOutput(self, name): |
| 53 | return "(%s *)memcpy(%s__out__, %s__in__, %s__len__)" % \ |
| 54 | (self.datatype, name, name, name) |
| 55 | |
| 56 | |
| 57 | class VarHeapCombinedInputOutputBufferType(HeapInputOutputBufferType): |
| 58 | |
| 59 | """same as base class, but passed as (inoutbuffer, &size)""" |
| 60 | |
| 61 | def passOutput(self, name): |
| 62 | return "(%s *)memcpy(%s__out__, %s__in__, &%s__len__)" % \ |
| 63 | (self.datatype, name, name, name) |
| 64 | |
| 65 | |
| 66 | class HeapOutputBufferType(OutputOnlyMixIn, HeapInputOutputBufferType): |
| 67 | |
| 68 | """Output buffer allocated on the heap -- passed as (buffer, size). |
| 69 | |
| 70 | Instantiate without parameters. |
| 71 | Call from Python with buffer size. |
| 72 | """ |
| 73 | |
| 74 | def declareInputBuffer(self, name): |
| 75 | pass |
| 76 | |
| 77 | def getargsFormat(self): |
| 78 | return self.sizeformat |
| 79 | |
| 80 | def getargsArgs(self, name): |
| 81 | return "&%s__len__" % name |
| 82 | |
| 83 | def passOutput(self, name): |
| 84 | return "%s__out__, %s__len__" % (name, name) |
| 85 | |
| 86 | |
| 87 | class VarHeapOutputBufferType(HeapOutputBufferType): |
| 88 | |
| 89 | """Output buffer allocated on the heap -- passed as (buffer, &size). |
| 90 | |
| 91 | Instantiate without parameters. |
| 92 | Call from Python with buffer size. |
| 93 | """ |
| 94 | |
| 95 | def passOutput(self, name): |
| 96 | return "%s__out__, &%s__len__" % (name, name) |
| 97 | |
| 98 | |
| 99 | class VarVarHeapOutputBufferType(VarHeapOutputBufferType): |
| 100 | |
| 101 | """Output buffer allocated on the heap -- passed as (buffer, size, &size). |
| 102 | |
| 103 | Instantiate without parameters. |
| 104 | Call from Python with buffer size. |
| 105 | """ |
| 106 | |
| 107 | def passOutput(self, name): |
| 108 | return "%s__out__, %s__len__, &%s__len__" % (name, name, name) |