Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "asdl.h" |
| 3 | |
| 4 | asdl_seq * |
| 5 | asdl_seq_new(int size) |
| 6 | { |
| 7 | asdl_seq *seq = NULL; |
| 8 | size_t n = sizeof(asdl_seq) + |
| 9 | (size ? (sizeof(void *) * (size - 1)) : 0); |
| 10 | |
| 11 | seq = (asdl_seq *)PyObject_Malloc(n); |
| 12 | if (!seq) { |
| 13 | PyErr_SetString(PyExc_MemoryError, "no memory"); |
| 14 | return NULL; |
| 15 | } |
| 16 | memset(seq, 0, n); |
| 17 | seq->size = size; |
| 18 | return seq; |
| 19 | } |
| 20 | |
| 21 | void |
| 22 | asdl_seq_free(asdl_seq *seq) |
| 23 | { |
| 24 | PyObject_Free(seq); |
| 25 | } |
| 26 | |
| 27 | #define CHECKSIZE(BUF, OFF, MIN) { \ |
| 28 | int need = *(OFF) + MIN; \ |
| 29 | if (need >= PyString_GET_SIZE(*(BUF))) { \ |
| 30 | int newsize = PyString_GET_SIZE(*(BUF)) * 2; \ |
| 31 | if (newsize < need) \ |
| 32 | newsize = need; \ |
| 33 | if (_PyString_Resize((BUF), newsize) < 0) \ |
| 34 | return 0; \ |
| 35 | } \ |
| 36 | } |
| 37 | |
| 38 | int |
| 39 | marshal_write_int(PyObject **buf, int *offset, int x) |
| 40 | { |
| 41 | char *s; |
| 42 | |
| 43 | CHECKSIZE(buf, offset, 4) |
| 44 | s = PyString_AS_STRING(*buf) + (*offset); |
| 45 | s[0] = (x & 0xff); |
| 46 | s[1] = (x >> 8) & 0xff; |
| 47 | s[2] = (x >> 16) & 0xff; |
| 48 | s[3] = (x >> 24) & 0xff; |
| 49 | *offset += 4; |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | int |
| 54 | marshal_write_bool(PyObject **buf, int *offset, bool b) |
| 55 | { |
| 56 | if (b) |
| 57 | marshal_write_int(buf, offset, 1); |
| 58 | else |
| 59 | marshal_write_int(buf, offset, 0); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | int |
| 64 | marshal_write_identifier(PyObject **buf, int *offset, identifier id) |
| 65 | { |
| 66 | int l = PyString_GET_SIZE(id); |
| 67 | marshal_write_int(buf, offset, l); |
| 68 | CHECKSIZE(buf, offset, l); |
| 69 | memcpy(PyString_AS_STRING(*buf) + *offset, |
| 70 | PyString_AS_STRING(id), l); |
| 71 | *offset += l; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | marshal_write_string(PyObject **buf, int *offset, string s) |
| 77 | { |
| 78 | int len = PyString_GET_SIZE(s); |
| 79 | marshal_write_int(buf, offset, len); |
| 80 | CHECKSIZE(buf, offset, len); |
| 81 | memcpy(PyString_AS_STRING(*buf) + *offset, |
| 82 | PyString_AS_STRING(s), len); |
| 83 | *offset += len; |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | int |
| 88 | marshal_write_object(PyObject **buf, int *offset, object s) |
| 89 | { |
| 90 | /* XXX */ |
| 91 | return 0; |
| 92 | } |