From: Laszlo Kajan <lkajan@rostlab.org>
Subject: 40000000 bug
 The default value 40000000 for FFINDEX_MAX_INDEX_ENTRIES_DEFAULT is too high for a machine with 2G (or less) RAM.
 Bug fixed by introducing an environment variable FFINDEX_MAX_INDEX_ENTRIES to control the value.
Forwarded: yes

--- a/src/ffindex.c
+++ b/src/ffindex.c
@@ -220,7 +220,7 @@
 ffindex_index_t* ffindex_index_parse(FILE *index_file, size_t num_max_entries)
 {
   if(num_max_entries == 0)
-    num_max_entries = FFINDEX_MAX_INDEX_ENTRIES_DEFAULT;
+    num_max_entries = ffindex_max_index_entries();
   size_t nbytes = sizeof(ffindex_index_t) + (sizeof(ffindex_entry_t) * num_max_entries);
   ffindex_index_t *index = (ffindex_index_t *)malloc(nbytes);
   if(index == NULL)
--- a/src/ffindex.h
+++ b/src/ffindex.h
@@ -18,6 +18,8 @@
 #include <stdio.h>
 
 #define FFINDEX_VERSION 0.961
+// lkajan: with ~72 bytes per entry, the below makes a default allocation over 2G. malloc in ffindex.c:225 fails on a machine with 'only' 2G of RAM.
+// lkajan: let the default remain 40000000, and let the actual value be controlled by the env var FFINDEX_MAX_INDEX_ENTRIES wherever FFINDEX_MAX_INDEX_ENTRIES_DEFAULT is used.
 #define FFINDEX_MAX_INDEX_ENTRIES_DEFAULT 40000000
 #define FFINDEX_MAX_ENTRY_NAME_LENTH 56
 
@@ -41,6 +43,8 @@
   ffindex_entry_t entries[]; /* This array is as big as the excess memory allocated for this struct. */
 } ffindex_index_t;
 
+inline size_t ffindex_max_index_entries(){ char *FMIE = getenv("FFINDEX_MAX_INDEX_ENTRIES"); if( FMIE ) { return atol( FMIE ); } else { return FFINDEX_MAX_INDEX_ENTRIES_DEFAULT; } }
+
 int ffindex_insert_memory(FILE *data_file, FILE *index_file, size_t *offset, char *from_start, size_t from_length, char *name);
 
 int ffindex_insert_file(FILE *data_file, FILE *index_file, size_t *offset, const char *path, char *name);
Binary files a/src/ffindex_apply and b/src/ffindex_apply differ
--- a/src/ffindex_apply.c
+++ b/src/ffindex_apply.c
@@ -34,7 +34,9 @@
   if(argn < 4)
   {
     fprintf(stderr, "USAGE: ffindex_apply DATA_FILENAME INDEX_FILENAME PROGRAM [PROGRAM_ARGS]*\n"
-                    "\nDesigned and implemented by Andy Hauser <hauser@genzentrum.lmu.de>.\n",
+                    "ENVIRONMENT\n"
+                    "\tFFINDEX_MAX_INDEX_ENTRIES - allocate memory for this number of entries\n"
+                    "\nDesigned and implemented by Andy Hauser <hauser@genzentrum.lmu.de>.\n"
     );
     return -1;
   }
--- a/src/ffindex_build.c
+++ b/src/ffindex_build.c
@@ -39,6 +39,8 @@
                     "\t\t-f can be specified up to %d times\n"
                     "\t-s\tsort index file\n"
                     "\t-v\tprint version and other info then exit\n"
+                    "ENVIRONMENT\n"
+                    "\tFFINDEX_MAX_INDEX_ENTRIES - allocate memory for this number of entries\n"
                     "\nDesigned and implemented by Andreas W. Hauser <hauser@genzentrum.lmu.de>.\n", MAX_FILENAME_LIST_FILES);
 }
 
--- a/src/ffindex_from_fasta.c
+++ b/src/ffindex_from_fasta.c
@@ -33,6 +33,8 @@
 {
     fprintf(stderr, "USAGE: ffindex_from_fasta -v | [-s] data_filename index_filename fasta_filename\n"
                     "\t-s\tsort index file\n"
+                    "ENVIRONMENT\n"
+                    "\tFFINDEX_MAX_INDEX_ENTRIES - allocate memory for this number of entries\n"
                     "\nDesigned and implemented by Andreas W. Hauser <hauser@genzentrum.lmu.de>.\n");
 }
 
--- a/src/ffindex_get.c
+++ b/src/ffindex_get.c
@@ -27,6 +27,8 @@
   if(argn < 3)
   {
     fprintf(stderr, "USAGE: ffindex_get data_filename index_filename filename(s)\n"
+                    "ENVIRONMENT\n"
+                    "\tFFINDEX_MAX_INDEX_ENTRIES - allocate memory for this number of entries\n"
                     "\nDesigned and implemented by Andreas W. Hauser <hauser@genzentrum.lmu.de>.\n");
     return -1;
   }
--- a/src/ffindex_modify.c
+++ b/src/ffindex_modify.c
@@ -37,6 +37,8 @@
                     "\t-t\tuse tree (default, will be removed soon)\n"
                     "\t-u\tunlink entry (remove from index only)\n"
                     "\t-v\tprint version and other info then exit\n"
+                    "ENVIRONMENT\n"
+                    "\tFFINDEX_MAX_INDEX_ENTRIES - allocate memory for this number of entries\n"
                     "\nDesigned and implemented by Andreas W. Hauser <hauser@genzentrum.lmu.de>.\n",
                     MAX_FILENAME_LIST_FILES);
 }
@@ -125,7 +127,7 @@
     }
     else
     {
-      char** sorted_names_to_unlink = malloc(FFINDEX_MAX_INDEX_ENTRIES_DEFAULT * sizeof(char *));
+      char** sorted_names_to_unlink = malloc(ffindex_max_index_entries() * sizeof(char *));
       if(sorted_names_to_unlink == NULL)
         fferror_print(__FILE__, __LINE__, __func__, "malloc failed");
       /* For each list_file unlink all entries */
