blob: c30d7d20599831fc9bf2ffd916c8ac60879a0dd6 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001#include "Python.h"
2#include "asdl.h"
3
4asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00005asdl_seq_new(int size, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00007 asdl_seq *seq = NULL;
8 size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00009
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000010 /* 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 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000016
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000017 /* check if size can be added safely */
18 if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
19 PyErr_NoMemory();
20 return NULL;
21 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000022
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000023 n += sizeof(asdl_seq);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000025 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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033}
34
35asdl_int_seq *
36asdl_int_seq_new(int size, PyArena *arena)
37{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000038 asdl_int_seq *seq = NULL;
39 size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000040
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000041 /* 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 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000047
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000048 /* check if size can be added safely */
49 if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
50 PyErr_NoMemory();
51 return NULL;
52 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000053
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000054 n += sizeof(asdl_seq);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000056 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 Hylton3e0055f2005-10-20 19:59:25 +000064}