/**
  Template Java/C wrapper code for a DSP Library.

  Substitute <libname> for DSPLib and modify arguments for the work() method.

  @author Jeff Schoen
  @version $Id: DSPLib.c,v 1.8 2011/07/27 18:46:03 ntn Exp $
*/
#include "cdefs.h"
#include "native.h"

#ifndef LIBNAME
#include "libDSP.c"
#define LIBNAME DSPLib
#define JMETHOD(M) Java_nxm_dsp_lib_DSPLib_ ## M
#define CMETHOD(M) DSPLib_ ## M
#define LIBWORKBUFS 2
#endif

#define PTR (void*)
#define CPTR (void*)(jpointer)
#define JPTR (jlong)(jpointer)

void* CMETHOD(alloc)();
int_4 CMETHOD(setkey)(void* handle, int_4 key, void* buf, int_4 len);
int_4 CMETHOD(getkey)(void* handle, int_4 key, void* buf, int_4 len);
int_4 CMETHOD(init)(void* handle);
int_4 CMETHOD(free)(void* handle);

#if   LIBWORKBUFS == 0
int_4 CMETHOD(work)(void* handle);
#elif LIBWORKBUFS == 1
int_4 CMETHOD(work)(void* handle, void* buf1, int_4 n);
#elif LIBWORKBUFS == 2
int_4 CMETHOD(work)(void* handle, void* buf1, void* buf2, int_4 n);
#elif LIBWORKBUFS == 3
int_4 CMETHOD(work)(void* handle, void* buf1, void* buf2, void* buf3, int_4 n);
#endif

JNIEXPORT jlong JNICALL JMETHOD(alloc) (JNIEnv *env, jobject obj) {
  return JPTR CMETHOD(alloc)();
}

JNIEXPORT jint  JNICALL JMETHOD(setkey) (JNIEnv *env, jobject obj, jlong handle,
		jint key, jobject buffer, jint len) {
  jbyte *buf = GetArray(buffer);
  jint status = (jint)CMETHOD(setkey) (CPTR handle,key,(void*)buf,len);
  ReleaseArray(buffer,buf);
  return status;
}

JNIEXPORT jint  JNICALL JMETHOD(getkey) (JNIEnv *env, jobject obj, jlong handle,
		jint key, jobject buffer, jint len) {
  jbyte *buf = GetArray(buffer);
  jint status = (jint)CMETHOD(getkey) (CPTR handle,key,(void*)buf,len);
  ReleaseArray(buffer,buf);
  return status;
}

JNIEXPORT jint  JNICALL JMETHOD(init) (JNIEnv *env, jobject obj, jlong handle) {
  return (jint)CMETHOD(init) (CPTR handle);
}

JNIEXPORT jint  JNICALL JMETHOD(free) (JNIEnv *env, jobject obj, jlong handle) {
  return (jint)CMETHOD(free) (CPTR handle);
}

#if   LIBWORKBUFS == 0
JNIEXPORT jint  JNICALL JMETHOD(work) (JNIEnv *env, jobject obj, jlong handle) {
  return (jint)CMETHOD(work) (CPTR handle);
}

#elif LIBWORKBUFS == 1
JNIEXPORT jint  JNICALL JMETHOD(work) (JNIEnv *env, jobject obj, jlong handle,
		jobject buffer1, jint todo) {
  jbyte *buf1 = GetArray(buffer1);
  jint status = (jint)CMETHOD(work) (CPTR handle,PTR buf1,todo);
  ReleaseArray(buffer1,buf1);
  return status;
}

#elif LIBWORKBUFS == 2
JNIEXPORT jint  JNICALL JMETHOD(work) (JNIEnv *env, jobject obj, jlong handle,
		jobject buffer1, jobject buffer2, jint todo) {
  jbyte *buf1 = GetArray(buffer1);
  jbyte *buf2 = GetArray(buffer2);
  jint status = (jint)CMETHOD(work) (CPTR handle,PTR buf1,PTR buf2,todo);
  ReleaseArray(buffer1,buf1);
  ReleaseArray(buffer2,buf2);
  return status;
}

#elif LIBWORKBUFS == 3
JNIEXPORT jint  JNICALL JMETHOD(work) (JNIEnv *env, jobject obj, jlong handle,
		jobject buffer1, jobject buffer2, jobject buffer3, jint todo) {
  jbyte *buf1 = GetArray(buffer1);
  jbyte *buf2 = GetArray(buffer2);
  jbyte *buf3 = GetArray(buffer3);
  jint status = (jint)CMETHOD(work) (CPTR handle,PTR buf1,PTR buf2,PTR buf3,todo);
  ReleaseArray(buffer1,buf1);
  ReleaseArray(buffer2,buf2);
  ReleaseArray(buffer3,buf3);
  return status;
}

#endif
