/* Cria um tipo no com valor arbitrario */ Node* NewNode(int Val){ Node* New = (Node*)Smalloc(sizeof(Node)); New->val = Val; return New; }
/* Cria umq fila */ Queue* NewQueue(){ Queue* Qe = Smalloc(sizeof(Queue)); Qe->start = NULL; Qe->end = NULL; return Qe; }
/* create charattrs and initialize first n elements */ void check_charattrs(String *str, int n, cattr_t cattrs, const char *file, int line) { if (!str->charattrs) { cattrs &= F_HWRITE; str->charattrs = Smalloc(str, sizeof(cattr_t) * str->size); while (--n >= 0) str->charattrs[n] = cattrs; } }
Solution* NewSolution(int n){ Solution* Sol = (Solution*)Smalloc(sizeof(Solution)); Sol->size = n; Sol->iters = Sol->nQueens = Sol->nKnights = 0; /* Inicializa filas de posições referentes a colunas */ Sol->queens = (Queue**)Smalloc(n*sizeof(Queue*)); Sol->knights = (Queue**)Smalloc(n*sizeof(Queue*)); int i; /* Inicializa cada fila de coluna */ for(i=0; i<n; i++){ Sol->queens[i] = NewQueue(); Sol->knights[i] = NewQueue(); } return Sol; }
/* dSinit() * data && len >= 0: allocate exactly len, copy data * data && len < 0: allocate exactly strlen(data), copy data * !data && len < 0: don't allocate * !data && len >= 0: allocate rounded len (allocating for 0 sometimes * wastes an allocation, but not doing it can cause * problems in code that expects (str->data != NULL)) * md is used only if (!str && data). */ String *dSinit( String *str, /* if NULL, a String will be allocated */ const char *data, /* optional initializer data */ int len, /* optional length of data */ attr_t attrs, /* line display attributes */ void *md, /* mmalloc descriptor */ const char *file, int line) { if (data && len < 0) len = strlen(data); if (!str) { if (data) { /* allocate String and data in one chunk for better locality */ #if USE_MMALLOC if (md) str = dmalloc(md, sizeof(*str) + len + 1, file, line); if (!md || !str) #endif { md = NULL; str = xmalloc(NULL, sizeof(*str) + len + 1, file, line); } str->data = (char*)str + sizeof(*str); str->dynamic_data = 0; } else { palloc(str, String, Stringpool, data, file, line); str->dynamic_data = 1; } str->dynamic_struct = 1; str->static_struct = 0; } else { str->dynamic_struct = 0; str->static_struct = 0; if (data) { str->data = Smalloc(str, len + 1); str->dynamic_data = 1; } } if (data) { #if USE_MMALLOC str->md = md; #endif str->resizable = 0; str->len = len; str->size = len + 1; memcpy(str->data, data, str->len); str->data[str->len] = '\0'; } else if (len >= 0) { #if USE_MMALLOC str->md = NULL; #endif str->resizable = 1; str->dynamic_data = 1; str->size = ((len + ALLOCSIZE) / ALLOCSIZE) * ALLOCSIZE; str->data = Smalloc(str, str->size); str->len = 0; str->data[str->len] = '\0'; } else { #if USE_MMALLOC str->md = NULL; #endif str->resizable = 1; str->dynamic_data = 1; str->data = NULL; str->size = str->len = 0; } str->attrs = attrs; str->charattrs = NULL; str->links = 0; str->time.tv_sec = str->time.tv_usec = -1; /* caller will set if needed */ str->file = file; str->line = line; return str; }