Skip to content

Instantly share code, notes, and snippets.

@pwaller
Created January 26, 2015 21:16
Show Gist options
  • Save pwaller/b805ddc57de281bc1659 to your computer and use it in GitHub Desktop.
Save pwaller/b805ddc57de281bc1659 to your computer and use it in GitHub Desktop.
Trying to use jemalloc with cgo
FROM ubuntu:14.04
RUN apt-get update && apt-get install -y curl build-essential git mercurial upx
RUN curl -sSL https://golang.org/dl/go1.4.src.tar.gz | tar -v -C /usr/local -xz
RUN cd /usr/local/go/src && ./make.bash --no-clean 2>&1
ENV GOPATH /go
ENV PATH /usr/local/go/bin:$PATH
ENV PATH /go/bin:$PATH
# reinstall standard library with netgo
RUN go clean -i net && go install -tags netgo std
# Change this date to invalidate the `go get` cache
RUN echo invalidate-on-20150120
ENV CGO_LDFLAGS="-L/go/src/pwaller.net/package -lpthread -ljemalloc_pic -lstdc++ -lm"
COPY . /go/src/pwaller.net/package
RUN go install \
-x \
-tags "netgo" \
-ldflags '-linkmode external -extldflags "-static -Wl,-v -Wl,--eh-frame-hdr"' \
pwaller.net/package
// Taken directly from:
// http://www.canonware.com/pipermail/jemalloc-discuss/2013-July/000617.html
/* Prototypes for __malloc_hook, __free_hook */
#include <malloc.h>
/* Prototypes for our hooks. */
static void my_init_hook (void);
static void *my_malloc_hook (size_t, const void *);
static void my_free_hook (void*, const void *);
/* Override ominitializing hook from the C library. */
void (*__MALLOC_HOOK_VOLATILE __malloc_initialize_hook) (void) = my_init_hook;
static void
my_init_hook (void)
{
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
}
void* je_malloc(size_t size);
void je_free(void *ptr);
static void *
my_malloc_hook (size_t size, const void *caller)
{
return je_malloc(size);
}
static void
my_free_hook (void *ptr, const void *caller)
{
je_free(ptr);
}
package main
// #include <stdlib.h>
import "C"
import "log"
func main() {
x := C.malloc(10)
defer C.free(x)
log.Printf("Hello world %p", x)
}
Stack trace: (no other threads running)
Program received signal SIGSEGV, Segmentation fault.
0x000000000051065c in get_nprocs ()
(gdb) bt
#0 0x000000000051065c in get_nprocs ()
#1 0x000000000050d7b5 in sysconf ()
#2 0x000000000049d9b6 in malloc_ncpus () at ../src/jemalloc.c:256
#3 malloc_init_hard () at ../src/jemalloc.c:776
#4 0x000000000049e32d in malloc_init () at ../src/jemalloc.c:292
#5 je_malloc (size=<optimised out>) at ../src/jemalloc.c:929
#6 0x0000000000541753 in _dl_get_origin ()
#7 0x0000000000511f0f in _dl_non_dynamic_init ()
#8 0x0000000000512e08 in __libc_init_first ()
#9 0x00000000004d5d82 in __libc_start_main ()
#10 0x00000000004019a7 in _start ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment