Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /trunk/sml3d/src/raw-data/c-alloc.sml
ViewVC logotype

View of /trunk/sml3d/src/raw-data/c-alloc.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 733 - (download) (annotate)
Sat Jan 23 20:51:00 2010 UTC (9 years, 8 months ago) by jhr
File size: 1186 byte(s)
  Moving to new C_POINTER interface and added CObject module
(* c-alloc.sml
 *
 * COPYRIGHT (c) 2008 John Reppy (http://www.cs.uchicago.edu/~jhr)
 * All rights reserved.
 *
 * Support for managing data in the C heap.  We provide both an interface
 * to malloc/free and support for traced pointers.
 *)

signature C_ALLOC =
  sig

  (* C pointer type *)
    type c_pointer = CPtr.t

    exception NoMemory

  (* allocate an object; raise Size on 0 and NoMemory if it cannot
   * allocate memory.
   *)
    val malloc : CSize.word -> c_pointer

  (* free an object *)
    val free : c_pointer -> unit

  end

structure CAlloc :> C_ALLOC = struct

    structure Final = MLton.Finalizable

  (* C pointer type *)
    type c_pointer = CPtr.t

  (* C memory allocation functions.  Note that on MinGW, these functions use
   * the "cdecl" calling convention.
   *)
    val primMalloc = _import "malloc" cdecl : C_Size.word -> CPtr.t;
    val primFree = _import "free" cdecl : CPtr.t -> unit;

    exception NoMemory

    fun malloc 0w0 = raise Size
      | malloc sz = let
	  val ptr = primMalloc sz
	  in
	    if CPtr.null = ptr
	      then raise NoMemory
	      else ptr
	  end

    fun free ptr = if (CPtr.null = ptr) then () else primFree ptr

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0