_WCRTLINK void _WCNEAR *sbrk( int increment ) { if( increment > 0 ) { void* p; increment = ( increment + 0x0fff ) & ~0x0fff; //p = LocalAlloc( LMEM_FIXED, increment ); //p = VirtualAlloc(NULL, increment, MEM_COMMIT,PAGE_EXECUTE_READWRITE); p = user_alloc(increment); if( p != NULL ) return( p ); errno = ENOMEM; } else { errno = EINVAL; } return( (void *) -1 ); }
static struct sw_displaytarget * kos_sw_displaytarget_create(struct sw_winsys *winsys, unsigned tex_usage, enum pipe_format format, unsigned width, unsigned height, unsigned alignment, unsigned *stride) { struct kos_sw_displaytarget *gdt; unsigned cpp; unsigned bpp; gdt = CALLOC_STRUCT(kos_sw_displaytarget); if(!gdt) goto no_gdt; gdt->format = format; gdt->width = width; gdt->height = height; bpp = util_format_get_blocksizebits(format); cpp = util_format_get_blocksize(format); gdt->stride = align(width * cpp, alignment); gdt->size = gdt->stride * height; gdt->data = user_alloc(gdt->size); if(!gdt->data) goto no_data; *stride = gdt->stride; return (struct sw_displaytarget *)gdt; no_data: FREE(gdt); no_gdt: return NULL; }
static int __AdjustAmount( unsigned *amount ) { unsigned old_amount = *amount; unsigned amt; #if ! ( defined(__WINDOWS_286__) || \ defined(__WINDOWS_386__) || \ defined(__WARP__) || \ defined(__NT__) \ ) unsigned last_free_amt; #endif amt = old_amount; amt = ( amt + TAG_SIZE + ROUND_SIZE) & ~ROUND_SIZE; if( amt < old_amount ) { return( 0 ); } #if ! ( defined(__WINDOWS_286__) || \ defined(__WINDOWS_386__) || \ defined(__WARP__) || \ defined(__NT__) \ ) #if defined(__DOS_EXT__) if( _IsRationalZeroBase() || _IsCodeBuilder() ) { // Allocating extra to identify the dpmi block amt += sizeof(struct dpmi_hdr); } else { #else { #endif last_free_amt = __LastFree(); /* adjust for last free block */ if( last_free_amt >= amt ) { amt = 0; } else { amt -= last_free_amt; } } #endif /* amount is even here */ /* extra amounts (22-feb-91 AFS) (1) adding a new heap needs: frl free block req'd for _nmalloc request (frl is the MINIMUM because the block may be freed) tag end of miniheap descriptor struct miniheapblkp start of miniheap descriptor (2) extending heap needs: tag free block req'd for _nmalloc request */ *amount = amt; amt += ( (TAG_SIZE) + sizeof(frl) + sizeof(struct miniheapblkp) ); if( amt < *amount ) return( 0 ); if( amt < _amblksiz ) { /* _amblksiz may not be even so round down to an even number nb. pathological case: where _amblksiz == 0xffff, we don't want the usual round up to even */ amt = _amblksiz & ~1u; } #if defined(__WINDOWS_386__) || \ defined(__WARP__) || \ defined(__NT__) || \ defined(__CALL21__) || \ defined(__DOS_EXT__) /* make sure amount is a multiple of 4k */ *amount = amt; amt += 0x0fff; if( amt < *amount ) return( 0 ); amt &= ~0x0fff; #endif *amount = amt; return( *amount != 0 ); } #if defined(__WINDOWS_286__) || \ defined(__WINDOWS_386__) || \ defined(__WARP__) || \ defined(__NT__) || \ defined(__CALL21__) || \ defined(__DOS_EXT__) static int __CreateNewNHeap( unsigned amount ) { mheapptr p1; frlptr flp; unsigned brk_value; if( !__heap_enabled ) return( 0 ); if( _curbrk == ~1u ) return( 0 ); if( __AdjustAmount( &amount ) == 0 ) return( 0 ); #if defined(__WINDOWS_286__) brk_value = (unsigned) LocalAlloc( LMEM_FIXED, amount ); if( brk_value == 0 ) { return( 0 ); } #elif defined(__WINDOWS_386__) brk_value = (unsigned) DPMIAlloc( amount ); if( brk_value == 0 ) { return( 0 ); } #elif defined(__WARP__) { PBYTE p; if( DosAllocMem( &p, amount, PAG_COMMIT|PAG_READ|PAG_WRITE ) ) { return( 0 ); } brk_value = (unsigned)p; } #elif defined(__NT__) // brk_value = (unsigned) VirtualAlloc( NULL, amount, MEM_COMMIT, // PAGE_EXECUTE_READWRITE ); brk_value = (unsigned) user_alloc(amount ); //brk_value = (unsigned) LocalAlloc( LMEM_FIXED, amount ); if( brk_value == 0 ) { return( 0 ); } #elif defined(__CALL21__) { tag _WCNEAR *tmp_tag; tmp_tag = (tag _WCNEAR *)TinyMemAlloc( amount ); if( tmp_tag == NULL ) { return( 0 ); } /* make sure it will not look like the end of a heap */ tmp_tag[0] = ! END_TAG; brk_value = (unsigned) &tmp_tag[2]; amount -= 2 * TAG_SIZE; // 11-jun-95, subtract extra tag } #elif defined(__DOS_EXT__) // if( _IsRationalZeroBase() || _IsCodeBuilder() ) { { tag *tmp_tag; if( _IsRational() ) { tmp_tag = RationalAlloc( amount ); if( tmp_tag ) amount = *tmp_tag; } else { /* CodeBuilder */ tmp_tag = TinyCBAlloc( amount ); amount -= TAG_SIZE; } if( tmp_tag == NULL ) { return( 0 ); } brk_value = (unsigned) tmp_tag; } // Pharlap, RSI/non-zero can never call this function #endif if( amount - TAG_SIZE > amount ) { return( 0 ); } else { amount -= TAG_SIZE; } if( amount < sizeof( struct miniheapblkp ) + sizeof( frl ) ) { /* there isn't enough for a heap block (struct miniheapblkp) and one free block (frl) */ return( 0 ); } /* we've got a new heap block */ p1 = (mheapptr) brk_value; p1->len = amount; // Now link it up flp = __LinkUpNewMHeap( p1 ); amount = flp->len; /* build a block for _nfree() */ flp->len = amount | 1; ++p1->numalloc; /* 28-dec-90 */ p1->largest_blk = 0; _nfree( (PTR)flp + TAG_SIZE ); return( 1 ); }
int http_load_file(const char *path, const char *url) { http_t *http; int received = 0; int offset = 0; int tail; char *buf; int fd; int i; buf = user_alloc(BUFFSIZE); for(i = 0; i < 16; i++) buf[i*4096] = 0; fd = open(path, O_CREAT|O_WRONLY); if(fd == -1) { user_free(buf); return 0; }; http = http_get(url, NULL,FLAG_STREAM|FLAG_REUSE_BUFFER, NULL); if(http == NULL) goto err_get; do { if(http_receive_with_retry(http, 500) == 0) { int count; // if(http->flags & 0xffff0000) // break; count = http->content_received - received; if(count+offset <= BUFFSIZE) { memcpy(buf+offset, http->content_ptr, count); offset+= count; } else { tail = count+offset-BUFFSIZE; count = BUFFSIZE - offset; if(count) { memcpy(buf+offset, http->content_ptr, count); offset = 0; }; write(fd, buf, BUFFSIZE); if(tail) { memcpy(buf, http->content_ptr+count, tail); offset = tail; } sprintf(conbuf, "%d bytes loaded\r", http->content_received); con_write_asciiz(conbuf); } received = http->content_received; } else break; } while( (http->flags & FLAG_GOT_ALL_DATA) == 0); if(offset) { write(fd, buf, offset); } // ftruncate(fd, received); close(fd); if(http->content_ptr) user_free(http->content_ptr); http_free(http); user_free(buf); return received; err_get: printf("HTTP GET failed\n"); return received; }