scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

newobj.c (437B)


      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 	if ((*obj->ops->new)(obj) < 0) {
     28 		free(obj);
     29 		return NULL;
     30 	}
     31 
     32 	return obj;
     33 }