Stdlib.hx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package cpp;
  2. @:include("stdlib.h")
  3. extern class Stdlib
  4. {
  5. @:native("malloc")
  6. public static function nativeMalloc(bytes:Int) : cpp.RawPointer<Void> return null;
  7. @:native("calloc")
  8. public static function nativeCalloc(bytes:Int) : cpp.RawPointer<Void> return null;
  9. @:native("realloc")
  10. public static function nativeRealloc(inPtr:cpp.RawPointer<Void>,bytes:Int) : cpp.RawPointer<Void> return null;
  11. @:native("free")
  12. public static function nativeFree(ptr:cpp.RawPointer<Void>) : Void { }
  13. // This needs a special compiler hack to avoid getting the size of a class pointer
  14. @:native("sizeof")
  15. public static function sizeof<T>(t:T) : Int { }
  16. inline public static function malloc<T>(bytes:Int) : cpp.Pointer<T>
  17. return cast nativeMalloc(bytes);
  18. inline public static function calloc<T>(bytes:Int) : cpp.Pointer<T>
  19. return cast nativeCalloc(bytes);
  20. inline public static function realloc<T>(ioPtr:cpp.Pointer<T>, bytes:Int) : Void
  21. ioPtr.setRaw( nativeRealloc(cast ioPtr.ptr, bytes) );
  22. inline public static function free<T>(ptr:cpp.Pointer<T>) : Void
  23. {
  24. if (ptr!=null)
  25. {
  26. nativeFree(cast ptr.ptr);
  27. ptr.ptr = null;
  28. }
  29. }
  30. }