Home | History | Annotate | Download | only in memconsumer
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <android/log.h>
      6 #include <jni.h>
      7 #include <stdio.h>
      8 #include <string.h>
      9 
     10 extern "C" {
     11 JNIEXPORT void JNICALL
     12     Java_org_chromium_memconsumer_ResidentService_nativeUseMemory(JNIEnv* env,
     13                                                                   jobject clazz,
     14                                                                   jlong memory);
     15 }
     16 
     17 namespace {
     18 
     19 uint32_t get_random() {
     20   static uint32_t m_w = 1;
     21   static uint32_t m_z = 1;
     22   m_z = 36969 * (m_z & 65535) + (m_z >> 16);
     23   m_w = 18000 * (m_w & 65535) + (m_w >> 16);
     24   return (m_z << 16) + m_w;
     25 }
     26 
     27 }  // namespace
     28 
     29 JNIEXPORT void JNICALL
     30     Java_org_chromium_memconsumer_ResidentService_nativeUseMemory(
     31         JNIEnv* env,
     32         jobject clazz,
     33         jlong memory) {
     34   static uint32_t* g_memory = NULL;
     35   if (g_memory)
     36     free(g_memory);
     37   if (memory == 0) {
     38     g_memory = NULL;
     39     return;
     40   }
     41   g_memory = static_cast<uint32_t*>(malloc(memory));
     42   if (!g_memory) {
     43     __android_log_print(ANDROID_LOG_WARN,
     44                         "MemConsumer",
     45                         "Unable to allocate %ld bytes",
     46                         memory);
     47   }
     48   // If memory allocation failed, try to allocate as much as possible.
     49   while (!g_memory) {
     50     memory /= 2;
     51     g_memory = static_cast<uint32_t*>(malloc(memory));
     52   }
     53   for (int i = 0; i < memory / sizeof(uint32_t); ++i)
     54     *(g_memory + i) = get_random();
     55 }
     56