Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "asdl.h" |
| 3 | |
| 4 | asdl_seq * |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 5 | asdl_seq_new(int size, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 7 | asdl_seq *seq = NULL; |
| 8 | size_t n = (size ? (sizeof(void *) * (size - 1)) : 0); |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 9 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 10 | /* check size is sane */ |
| 11 | if (size < 0 || size == INT_MIN || |
| 12 | (size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) { |
| 13 | PyErr_NoMemory(); |
| 14 | return NULL; |
| 15 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 16 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 17 | /* check if size can be added safely */ |
| 18 | if (n > PY_SIZE_MAX - sizeof(asdl_seq)) { |
| 19 | PyErr_NoMemory(); |
| 20 | return NULL; |
| 21 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 22 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 23 | n += sizeof(asdl_seq); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 24 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 25 | seq = (asdl_seq *)PyArena_Malloc(arena, n); |
| 26 | if (!seq) { |
| 27 | PyErr_NoMemory(); |
| 28 | return NULL; |
| 29 | } |
| 30 | memset(seq, 0, n); |
| 31 | seq->size = size; |
| 32 | return seq; |
Martin v. Löwis | 0cc56e5 | 2006-04-13 12:29:43 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | asdl_int_seq * |
| 36 | asdl_int_seq_new(int size, PyArena *arena) |
| 37 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 38 | asdl_int_seq *seq = NULL; |
| 39 | size_t n = (size ? (sizeof(void *) * (size - 1)) : 0); |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 40 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 41 | /* check size is sane */ |
| 42 | if (size < 0 || size == INT_MIN || |
| 43 | (size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) { |
| 44 | PyErr_NoMemory(); |
| 45 | return NULL; |
| 46 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 47 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 48 | /* check if size can be added safely */ |
| 49 | if (n > PY_SIZE_MAX - sizeof(asdl_seq)) { |
| 50 | PyErr_NoMemory(); |
| 51 | return NULL; |
| 52 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 53 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 54 | n += sizeof(asdl_seq); |
Martin v. Löwis | 0cc56e5 | 2006-04-13 12:29:43 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 56 | seq = (asdl_int_seq *)PyArena_Malloc(arena, n); |
| 57 | if (!seq) { |
| 58 | PyErr_NoMemory(); |
| 59 | return NULL; |
| 60 | } |
| 61 | memset(seq, 0, n); |
| 62 | seq->size = size; |
| 63 | return seq; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 64 | } |