bool Yap_CloseMemoryStream( int sno ) { if (!(GLOBAL_Stream[sno].status & Output_Stream_f) ) { #if MAY_WRITE fclose(GLOBAL_Stream[sno].file); if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f) free( GLOBAL_Stream[sno].nbuf ); #else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE) Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf); else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) { free(GLOBAL_Stream[sno].u.mem_string.buf); } #endif } else { #if MAY_READ fclose(GLOBAL_Stream[sno].file); Yap_FreeAtomSpace(GLOBAL_Stream[sno].nbuf); #else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE) Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf); else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) { free(GLOBAL_Stream[sno].u.mem_string.buf); } #endif } return true; }
static int ExpandPredHash(void) { UInt new_size = PredHashTableSize + PredHashIncrement; PredEntry **oldp = PredHash; PredEntry **np = (PredEntry **)Yap_AllocAtomSpace(sizeof(PredEntry **) * new_size); UInt i; if (!np) { return FALSE; } for (i = 0; i < new_size; i++) { np[i] = NULL; } for (i = 0; i < PredHashTableSize; i++) { PredEntry *p = PredHash[i]; while (p) { PredEntry *nextp = p->NextPredOfHash; UInt hsh = PRED_HASH(p->FunctorOfPred, p->ModuleOfPred, new_size); p->NextPredOfHash = np[hsh]; np[hsh] = p; p = nextp; } } PredHashTableSize = new_size; PredHash = np; Yap_FreeAtomSpace((ADDR)oldp); return TRUE; }
/* static */ static int MemPutc(int sno, int ch) { StreamDesc *s = &GLOBAL_Stream[sno]; #if MAC || _MSC_VER if (ch == 10) { ch = '\n'; } #endif s->u.mem_string.buf[s->u.mem_string.pos++] = ch; if (s->u.mem_string.pos >= s->u.mem_string.max_size -8) { int old_src = s->u.mem_string.src, new_src; /* oops, we have reached an overflow */ Int new_max_size = s->u.mem_string.max_size + Yap_page_size; char *newbuf; if (old_src == MEM_BUF_CODE && (newbuf = Yap_AllocAtomSpace(new_max_size*sizeof(char))) != NULL) { new_src = MEM_BUF_CODE; #if HAVE_MEMMOVE memmove((void *)newbuf, (void *)s->u.mem_string.buf, (size_t)((s->u.mem_string.pos)*sizeof(char))); #else { Int n = s->u.mem_string.pos; char *to = newbuf; char *from = s->u.mem_string.buf; while (n-- >= 0) { *to++ = *from++; } } #endif Yap_FreeAtomSpace(s->u.mem_string.buf); #if !HAVE_SYSTEM_MALLOC } else if ((newbuf = (ADDR)realloc(s->u.mem_string.buf, new_max_size*sizeof(char))) != NULL) { new_src = MEM_BUF_MALLOC; #endif } else { if (GLOBAL_Stream[sno].u.mem_string.error_handler) { CACHE_REGS LOCAL_Error_Size = new_max_size*sizeof(char); save_machine_regs(); longjmp(*(jmp_buf *)GLOBAL_Stream[sno].u.mem_string.error_handler,1); } else { Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "YAP could not grow heap for writing to string"); } return -1; } if (old_src == MEM_BUF_CODE) { } s->u.mem_string.buf = newbuf; s->u.mem_string.max_size = new_max_size; s->u.mem_string.src = new_src; } count_output_char(ch,s); return ((int) ch); }