newobj.c (462B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <errno.h> 5 6 #include <scc/mach.h> 7 8 #include "libmach.h" 9 10 Obj * 11 newobj(int type) 12 { 13 Obj *obj; 14 int fmt; 15 16 fmt = FORMAT(type); 17 if (fmt >= NFORMATS) { 18 errno = ERANGE; 19 return NULL; 20 } 21 22 if ((obj = malloc(sizeof(*obj))) == NULL) 23 return NULL; 24 25 obj->type = type; 26 obj->ops = objops[fmt]; 27 obj->next = NULL; 28 if ((*obj->ops->new)(obj, type) < 0) { 29 free(obj); 30 return NULL; 31 } 32 33 return obj; 34 }