Home | History | Annotate | Download | only in Python
      1 #include "Python.h"
      2 #include "asdl.h"
      3 
      4 asdl_seq *
      5 _Py_asdl_seq_new(Py_ssize_t size, PyArena *arena)
      6 {
      7     asdl_seq *seq = NULL;
      8     size_t n;
      9 
     10     /* check size is sane */
     11     if (size < 0 ||
     12         (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) {
     13         PyErr_NoMemory();
     14         return NULL;
     15     }
     16     n = (size ? (sizeof(void *) * (size - 1)) : 0);
     17 
     18     /* check if size can be added safely */
     19     if (n > SIZE_MAX - sizeof(asdl_seq)) {
     20         PyErr_NoMemory();
     21         return NULL;
     22     }
     23     n += sizeof(asdl_seq);
     24 
     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;
     33 }
     34 
     35 asdl_int_seq *
     36 _Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena)
     37 {
     38     asdl_int_seq *seq = NULL;
     39     size_t n;
     40 
     41     /* check size is sane */
     42     if (size < 0 ||
     43         (size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) {
     44             PyErr_NoMemory();
     45             return NULL;
     46     }
     47     n = (size ? (sizeof(void *) * (size - 1)) : 0);
     48 
     49     /* check if size can be added safely */
     50     if (n > SIZE_MAX - sizeof(asdl_seq)) {
     51         PyErr_NoMemory();
     52         return NULL;
     53     }
     54     n += sizeof(asdl_seq);
     55 
     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;
     64 }
     65