File: cdata.i

package info (click to toggle)
swig 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 46,232 kB
  • sloc: cpp: 54,631; ansic: 29,122; java: 17,530; python: 12,505; cs: 10,369; ruby: 7,232; yacc: 6,477; makefile: 5,965; javascript: 5,520; sh: 5,415; perl: 4,187; php: 3,693; ml: 2,187; lisp: 2,056; tcl: 1,991; xml: 115
file content (57 lines) | stat: -rw-r--r-- 1,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* -----------------------------------------------------------------------------
 * cdata.i
 *
 * SWIG library file containing macros for manipulating raw C data.
 * ----------------------------------------------------------------------------- */

/* ------------------------------------------------------------
 * Typemap for passing bytes with length
 * ------------------------------------------------------------ */

%typemap(gotype) (const void *BYTES, size_t LENGTH) "[]byte"
%typemap(in) (const void *BYTES, size_t LENGTH) %{
  $1 = ($1_ltype)$input.array;
  $2 = ($2_ltype)$input.len;
%}
%apply (const void *BYTES, size_t LENGTH) { (void *BYTES, size_t LENGTH) }

%include <typemaps/cdata_begin.swg>

%fragment("cdata", "header") %{
struct swigcdata {
  uintgo size;
  void *data;
};
%}

%typemap(gotype) SWIGCDATA "[]byte"
%typemap(imtype) SWIGCDATA "uint64"

%typemap(out, fragment="cdata") SWIGCDATA(struct swigcdata *swig_out) %{
  swig_out = (struct swigcdata *)malloc(sizeof(*swig_out));
  if (swig_out) {
    swig_out->size = $1.len;
    swig_out->data = malloc(swig_out->size);
    if (swig_out->data) {
      memcpy(swig_out->data, $1.data, swig_out->size);
    }
  }
  $result = *(long long *)(void **)&swig_out;
%}

%typemap(goout) SWIGCDATA {
  type swigcdata struct { size int; data uintptr }
  p := (*swigcdata)(unsafe.Pointer(uintptr($1)))
  if p == nil || p.data == 0 {
    $result = nil
  } else {
    b := make([]byte, p.size)
    a := (*[0x7fffffff]byte)(unsafe.Pointer(p.data))[:p.size]
    copy(b, a)
    Swig_free(p.data)
    Swig_free(uintptr(unsafe.Pointer(p)))
    $result = b
  }
}

%include <typemaps/cdata_end.swg>