calloc

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 2 Imported by: 0

README

calloc

A simple, dependency free wrapper for managing your C string allocations in Go.

Installation

go get -u github.com/leaanthony/calloc

Usage

  1. Create a new allocator: c := calloc.New()
  2. Defer the free method: defer c.Free()
  3. Start creating C strings: myCString := c.String("I am a C string")

Why?

It's a common pattern when using CGO to allocate C strings, use them in a CGO call then deallocate them once the call is complete. This tiny module not only cleans up your code but ensures that all allocated memory gets freed.

NOTE: The library isn't thread safe as it is not intended to be shared between threads. It is better to create new allocators where needed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Calloc

type Calloc struct {
	// contains filtered or unexported fields
}

Calloc handles alloc/dealloc of C data

func New

func New() Calloc

New creates a new allocator

func (Calloc) Free

func (c Calloc) Free()

Free frees all allocated C memory

func (Calloc) String

func (c Calloc) String(in string) *C.char

String creates a new C string and retains a reference to it

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL