hal: disable unused feature to reduce memory footprint

    - Disable feature flags which are not needed to reduce memory
      footprint.
    - Release any unused memory once use case is over.

Change-Id: I7854b8f00748f204f38b08b9dfa3c00b2bbe0c2b
(cherry picked from commit a65e5b966816fa7168105e4d4e7ec1de83240ec9)
diff --git a/configs/bengal/bengal.mk b/configs/bengal/bengal.mk
index e3bc4e4..c46d9eb 100644
--- a/configs/bengal/bengal.mk
+++ b/configs/bengal/bengal.mk
@@ -15,16 +15,92 @@
 USE_CUSTOM_AUDIO_POLICY := 1
 AUDIO_FEATURE_QSSI_COMPLIANCE := true
 AUDIO_FEATURE_ENABLED_COMPRESS_CAPTURE := false
-AUDIO_FEATURE_ENABLED_COMPRESS_INPUT := true
-AUDIO_FEATURE_ENABLED_CONCURRENT_CAPTURE := true
 AUDIO_FEATURE_ENABLED_COMPRESS_VOIP := false
 AUDIO_FEATURE_ENABLED_DYNAMIC_ECNS := true
 AUDIO_FEATURE_ENABLED_EXTN_FORMATS := true
+AUDIO_FEATURE_ENABLED_PCM_OFFLOAD := true
+AUDIO_FEATURE_ENABLED_DTS_EAGLE := false
+BOARD_USES_SRS_TRUEMEDIA := false
+DTS_CODEC_M_ := false
+AUDIO_FEATURE_ENABLED_HW_ACCELERATED_EFFECTS := false
+AUDIO_FEATURE_ENABLED_A2DP_OFFLOAD := true
+AUDIO_FEATURE_ENABLED_AHAL_EXT := false
+DOLBY_ENABLE := false
+endif
+
+AUDIO_FEATURE_ENABLED_DLKM := true
+BOARD_SUPPORTS_GCS := false
+AUDIO_FEATURE_ENABLED_INSTANCE_ID := true
+AUDIO_USE_DEEP_AS_PRIMARY_OUTPUT := false
+AUDIO_FEATURE_ENABLED_VBAT_MONITOR := true
+AUDIO_FEATURE_ENABLED_NT_PAUSE_TIMEOUT := true
+AUDIO_FEATURE_ENABLED_CUSTOMSTEREO := true
+AUDIO_FEATURE_ENABLED_FLUENCE := true
+#AUDIO_FEATURE_ENABLED_KEEP_ALIVE := true
+AUDIO_FEATURE_ENABLED_DS2_DOLBY_DAP := false
+AUDIO_FEATURE_ENABLED_INCALL_MUSIC := true
+AUDIO_FEATURE_ENABLED_MULTI_VOICE_SESSIONS := true
+AUDIO_FEATURE_ENABLED_KPI_OPTIMIZE := true
+AUDIO_FEATURE_ENABLED_SPKR_PROTECTION := true
+AUDIO_FEATURE_ENABLED_ACDB_LICENSE := false
+AUDIO_FEATURE_ENABLED_DEV_ARBI := false
+MM_AUDIO_ENABLED_FTM := true
+TARGET_USES_QCOM_MM_AUDIO := true
+BOARD_SUPPORTS_QAHW := false
+AUDIO_FEATURE_ENABLED_RAS := true
+AUDIO_FEATURE_ENABLED_SND_MONITOR := true
+AUDIO_FEATURE_ENABLED_BATTERY_LISTENER := true
+##AUDIO_FEATURE_FLAGS
+
+ifeq ($(TARGET_1G_DDR_RAM), true)
+AUDIO_FEATURE_ENABLED_AUDIOSPHERE := false
+AUDIO_FEATURE_ENABLED_3D_AUDIO := false
+BOARD_SUPPORTS_SOUND_TRIGGER := false
+AUDIO_FEATURE_ENABLED_HFP := false
+AUDIO_FEATURE_ENABLED_SVA_MULTI_STAGE := false
+BOARD_SUPPORTS_OPENSOURCE_STHAL := false
+AUDIO_DO_NOT_USE_OMX_CODEC := true
+AUDIO_FEATURE_ENABLED_COMPRESS_INPUT := false
+AUDIO_FEATURE_ENABLED_CONCURRENT_CAPTURE := false
+AUDIO_FEATURE_ENABLED_EXTN_FLAC_DECODER := false
+AUDIO_FEATURE_ENABLED_EXTN_RESAMPLER := false
+AUDIO_FEATURE_ENABLED_FM_POWER_OPT := false
+AUDIO_FEATURE_ENABLED_HDMI_SPK := false
+AUDIO_FEATURE_ENABLED_PCM_OFFLOAD_24 := false
+AUDIO_FEATURE_ENABLED_FLAC_OFFLOAD := false
+AUDIO_FEATURE_ENABLED_VORBIS_OFFLOAD := false
+AUDIO_FEATURE_ENABLED_WMA_OFFLOAD := false
+AUDIO_FEATURE_ENABLED_ALAC_OFFLOAD := false
+AUDIO_FEATURE_ENABLED_APE_OFFLOAD := false
+AUDIO_FEATURE_ENABLED_AAC_ADTS_OFFLOAD := false
+AUDIO_FEATURE_ENABLED_MPEGH_SW_DECODER := false
+AUDIO_FEATURE_ENABLED_PROXY_DEVICE := false
+AUDIO_FEATURE_ENABLED_SSR := false
+MM_AUDIO_ENABLED_SAFX := false
+AUDIO_FEATURE_ENABLED_USB_TUNNEL := false
+AUDIO_FEATURE_ENABLED_EXTENDED_COMPRESS_FORMAT := false
+AUDIO_FEATURE_ENABLED_ANC_HEADSET := false
+AUDIO_FEATURE_ENABLED_HDMI_EDID := false
+AUDIO_FEATURE_ENABLED_HDMI_PASSTHROUGH := false
+AUDIO_FEATURE_ENABLED_DISPLAY_PORT := false
+AUDIO_FEATURE_ENABLED_DYNAMIC_LOG := false
+AUDIO_FEATURE_ENABLED_SOURCE_TRACKING := false
+AUDIO_FEATURE_ENABLED_GEF_SUPPORT := false
+AUDIO_FEATURE_ENABLED_USB_BURST_MODE := false
+PURGE_UNUSED_MEMORY := true
+else
+AUDIO_FEATURE_ENABLED_AUDIOSPHERE := true
+AUDIO_FEATURE_ENABLED_3D_AUDIO := true
+BOARD_SUPPORTS_SOUND_TRIGGER := true
+AUDIO_FEATURE_ENABLED_HFP := true
+AUDIO_FEATURE_ENABLED_SVA_MULTI_STAGE := true
+BOARD_SUPPORTS_OPENSOURCE_STHAL := true
+AUDIO_FEATURE_ENABLED_COMPRESS_INPUT := true
+AUDIO_FEATURE_ENABLED_CONCURRENT_CAPTURE := true
 AUDIO_FEATURE_ENABLED_EXTN_FLAC_DECODER := true
 AUDIO_FEATURE_ENABLED_EXTN_RESAMPLER := true
 AUDIO_FEATURE_ENABLED_FM_POWER_OPT := true
 AUDIO_FEATURE_ENABLED_HDMI_SPK := true
-AUDIO_FEATURE_ENABLED_PCM_OFFLOAD := true
 AUDIO_FEATURE_ENABLED_PCM_OFFLOAD_24 := true
 AUDIO_FEATURE_ENABLED_FLAC_OFFLOAD := true
 AUDIO_FEATURE_ENABLED_VORBIS_OFFLOAD := true
@@ -35,56 +111,18 @@
 AUDIO_FEATURE_ENABLED_MPEGH_SW_DECODER := true
 AUDIO_FEATURE_ENABLED_PROXY_DEVICE := true
 AUDIO_FEATURE_ENABLED_SSR := true
-AUDIO_FEATURE_ENABLED_DTS_EAGLE := false
-BOARD_USES_SRS_TRUEMEDIA := false
-DTS_CODEC_M_ := false
 MM_AUDIO_ENABLED_SAFX := true
-AUDIO_FEATURE_ENABLED_HW_ACCELERATED_EFFECTS := false
-AUDIO_FEATURE_ENABLED_AUDIOSPHERE := true
 AUDIO_FEATURE_ENABLED_USB_TUNNEL := true
-AUDIO_FEATURE_ENABLED_A2DP_OFFLOAD := true
-AUDIO_FEATURE_ENABLED_3D_AUDIO := true
-AUDIO_FEATURE_ENABLED_AHAL_EXT := false
 AUDIO_FEATURE_ENABLED_EXTENDED_COMPRESS_FORMAT := true
-DOLBY_ENABLE := false
-endif
-
-AUDIO_FEATURE_ENABLED_DLKM := true
-BOARD_SUPPORTS_SOUND_TRIGGER := true
-BOARD_SUPPORTS_GCS := false
-AUDIO_FEATURE_ENABLED_INSTANCE_ID := true
-AUDIO_USE_DEEP_AS_PRIMARY_OUTPUT := false
-AUDIO_FEATURE_ENABLED_VBAT_MONITOR := true
-AUDIO_FEATURE_ENABLED_NT_PAUSE_TIMEOUT := true
 AUDIO_FEATURE_ENABLED_ANC_HEADSET := true
-AUDIO_FEATURE_ENABLED_CUSTOMSTEREO := true
-AUDIO_FEATURE_ENABLED_FLUENCE := true
 AUDIO_FEATURE_ENABLED_HDMI_EDID := true
 AUDIO_FEATURE_ENABLED_HDMI_PASSTHROUGH := true
-#AUDIO_FEATURE_ENABLED_KEEP_ALIVE := true
 AUDIO_FEATURE_ENABLED_DISPLAY_PORT := true
-AUDIO_FEATURE_ENABLED_DS2_DOLBY_DAP := false
-AUDIO_FEATURE_ENABLED_HFP := true
-AUDIO_FEATURE_ENABLED_INCALL_MUSIC := true
-AUDIO_FEATURE_ENABLED_MULTI_VOICE_SESSIONS := true
-AUDIO_FEATURE_ENABLED_KPI_OPTIMIZE := true
-AUDIO_FEATURE_ENABLED_SPKR_PROTECTION := true
-AUDIO_FEATURE_ENABLED_ACDB_LICENSE := false
-AUDIO_FEATURE_ENABLED_DEV_ARBI := false
 AUDIO_FEATURE_ENABLED_DYNAMIC_LOG := true
-MM_AUDIO_ENABLED_FTM := true
-TARGET_USES_QCOM_MM_AUDIO := true
 AUDIO_FEATURE_ENABLED_SOURCE_TRACKING := true
 AUDIO_FEATURE_ENABLED_GEF_SUPPORT := true
-BOARD_SUPPORTS_QAHW := false
-AUDIO_FEATURE_ENABLED_RAS := true
-AUDIO_FEATURE_ENABLED_SND_MONITOR := true
 AUDIO_FEATURE_ENABLED_USB_BURST_MODE := true
-AUDIO_FEATURE_ENABLED_SVA_MULTI_STAGE := true
-AUDIO_FEATURE_ENABLED_BATTERY_LISTENER := true
-##AUDIO_FEATURE_FLAGS
-
-BOARD_SUPPORTS_OPENSOURCE_STHAL := true
+endif
 
 AUDIO_HARDWARE := audio.a2dp.default
 AUDIO_HARDWARE += audio.usb.default
diff --git a/hal/Android.mk b/hal/Android.mk
index ba6c536..820749b 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -417,6 +417,11 @@
     LOCAL_SHARED_LIBRARIES += vendor.qti.hardware.audiohalext@1.0
 endif
 
+# Memory optimization specific feature
+ifeq ($(strip $(TARGET_1G_DDR_RAM)), true)
+    LOCAL_CFLAGS += -DPURGE_UNUSED_MEM
+endif
+
 LOCAL_CFLAGS += -D_GNU_SOURCE
 LOCAL_CFLAGS += -Wall -Werror
 
diff --git a/hal/audio_extn/audio_extn.c b/hal/audio_extn/audio_extn.c
index 61ccfcb..2c677d6 100644
--- a/hal/audio_extn/audio_extn.c
+++ b/hal/audio_extn/audio_extn.c
@@ -92,6 +92,7 @@
 #include "platform_api.h"
 #include "edid.h"
 #include "sound/compress_params.h"
+#include <pthread.h>
 
 #ifdef AUDIO_GKI_ENABLED
 #include "sound/audio_compressed_formats.h"
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
index be20e12..55006d2 100644
--- a/hal/audio_extn/utils.c
+++ b/hal/audio_extn/utils.c
@@ -3315,7 +3315,7 @@
     return -EINVAL;
 }
 
-void audio_extn_utils_release_snd_device(snd_device_t snd_device)
+void audio_extn_utils_release_snd_device(snd_device_t snd_device __unused)
 {
     audio_extn_dev_arbi_release(snd_device);
     audio_extn_sound_trigger_update_device_status(snd_device,
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 8442a6b..94c75a8 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -3566,6 +3566,9 @@
     }
 
     enable_gcov();
+#ifdef PURGE_UNUSED_MEM
+    mallopt(M_PURGE, 0);
+#endif
     ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
 }
@@ -4205,6 +4208,9 @@
 
     clear_devices(&uc_info->device_list);
     free(uc_info);
+#ifdef PURGE_UNUSED_MEM
+    mallopt(M_PURGE, 0);
+#endif
     ALOGV("%s: exit: status(%d)", __func__, ret);
     return ret;
 }