/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.caffinitas.ohc.alloc;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class JEMallocAllocator implements IAllocator
{
    public interface JEMLibrary extends Library
    {
        long malloc(long size);

        void free(long pointer);
    }

    private final JEMLibrary library;
    
    public JEMallocAllocator()
    {
        library = (JEMLibrary) Native.loadLibrary("jemalloc", JEMLibrary.class);
    }

    public long allocate(long size)
    {
        return library.malloc(size);
    }

    public void free(long peer)
    {
        library.free(peer);
    }
}

@@ -834,16 +834,212 @@
 alloc;%0A%0A
+import java.lang.reflect.InvocationHandler;%0Aimport java.lang.reflect.Method;%0Aimport java.util.Collections;%0Aimport java.util.Map;%0A%0Aimport com.sun.jna.Function;%0Aimport com.sun.jna.InvocationMapper;%0A
 import c
@@ -1084,16 +1084,50 @@
 .Native;
+%0Aimport com.sun.jna.NativeLibrary;
 %0A%0Apublic
@@ -1374,32 +1374,1510 @@
 locator()%0A    %7B%0A
+        Map options = Collections.singletonMap(Library.OPTION_INVOCATION_MAPPER,%0A                                 // Invocation mapping%0A                                 new InvocationMapper() %7B%0A                                     public InvocationHandler getInvocationHandler(NativeLibrary lib, Method m) %7B%0A                                         final Function f = lib.getFunction(m.getName());%0A                                         if (%22malloc%22.equals(m.getName()))%0A                                             return new InvocationHandler() %7B%0A                                                 public Object invoke(Object proxy, Method method, Object%5B%5D args) %7B%0A                                                     return f.invokeLong(args);%0A                                                 %7D%0A                                             %7D;%0A                                         if (%22free%22.equals(m.getName()))%0A                                             return new InvocationHandler() %7B%0A                                                 public Object invoke(Object proxy, Method method, Object%5B%5D args) %7B%0A                                                     f.invoke(args);%0A                                                     return null;%0A                                                 %7D%0A                                             %7D;%0A                                         return null;%0A                                     %7D%0A                                 %7D );%0A
         library 
@@ -2938,16 +2938,25 @@
 ry.class
+, options
 );%0A    %7D

optimize jemalloc via JNA invocations (skip unnecessary mappings)