From: SMTP%"cowan@btgmax.zko.dec.com" 18-MAR-1994 11:52:14.90 To: EVERHART CC: Subj: example using lib$vm_malloc From: cowan@btgmax.zko.dec.com (Ken Cowan) X-Newsgroups: comp.os.vms Subject: example using lib$vm_malloc Date: 17 Mar 1994 14:16:54 GMT Organization: Digital Equipment Corporation Lines: 87 Distribution: world Message-ID: <2m9oom$2a9@jac.zko.dec.com> Reply-To: cowan@btgmax.zko.dec.com (Ken Cowan) NNTP-Posting-Host: btgmax.zko.dec.com X-Newsreader: mxrn 6.18-9 To: Info-VAX@CRVAX.SRI.COM X-Gateway-Source-Info: USENET I recieved a request to post the calling sequences for lib$vm_malloc and friends. With the exception of setting errno, lib$vm_malloc, lib$vm_calloc and lib$vm_realloc have the exact same calling sequence as the stnadard C library. lib$vm_free is somewhat different. Some old implementations of free() returned a status code. ANSI C defines free() as a void function. If you use the ANSI C definition, then ignore the return value from lib$vm_free. If you need a return value, be aware that lib$vm_free returns a VMS condition code. So, your private malloc package might look like the code below. Note that if you do not have the CMA TIS support (in an image SYS$LIBRARY:CMA$TIS_SHR), then change: cma$tis_errno_set_value( ENOMEM); to errno = ENOMEM; and cma$tis_vmserrno_set_value( status ); to vaxc$errno = status; [In case you are curious, the CMA TIS support is to implement thread local copies of errno. These calls work fine for single-threaded programs too] KC void *my_calloc(size_t nmemb, size_t size) { void *ptr; ptr = LIB$VM_CALLOC(nmemb,size); if (ptr == NULL) { cma$tis_errno_set_value( ENOMEM ); return NULL; } return ptr; } void *my_malloc(size_t size) { void *ptr; ptr = LIB$VM_MALLOC(size); if (ptr == NULL) { cma$tis_errno_set_value( ENOMEM ); return NULL; } return ptr; } void *my_realloc(void *oldptr, size_t size) { void *ptr; ptr = LIB$VM_REALLOC(oldptr, size); if (ptr == NULL) { cma$tis_errno_set_value( ENOMEM ); return NULL; } return ptr; } int my_free(void *ptr) { int status; if ((status = LIB$VM_FREE(ptr)) & 1) { return 0; } else { cma$tis_errno_set_value( EVMSERR ); cma$tis_vmserrno_set_value( status ); return -1; } } int my_cfree(void *ptr) { return my_free( ptr ); } -- Ken Cowan, ZK2-3/Q8 cowan@rtl.enet.dec.com Digital Equipment Corporation decwrl!rtl!cowan 110 Spit Brook Rd, Nashua, NH 03062