memory/mozalloc/tests/TestVolatileBuffer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memory/mozalloc/tests/TestVolatileBuffer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,144 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "TestHarness.h"
     1.9 +#include "mozilla/VolatileBuffer.h"
    1.10 +#include "mozilla/NullPtr.h"
    1.11 +#include <string.h>
    1.12 +
    1.13 +#if defined(ANDROID)
    1.14 +#include <fcntl.h>
    1.15 +#include <linux/ashmem.h>
    1.16 +#include <sys/ioctl.h>
    1.17 +#include <sys/stat.h>
    1.18 +#include <sys/types.h>
    1.19 +#elif defined(XP_DARWIN)
    1.20 +#include <mach/mach.h>
    1.21 +#endif
    1.22 +
    1.23 +using namespace mozilla;
    1.24 +
    1.25 +int main(int argc, char **argv)
    1.26 +{
    1.27 +  ScopedXPCOM xpcom("VolatileBufferTests");
    1.28 +  if (xpcom.failed()) {
    1.29 +    return 1;
    1.30 +  }
    1.31 +
    1.32 +  RefPtr<VolatileBuffer> heapbuf = new VolatileBuffer();
    1.33 +  if (!heapbuf || !heapbuf->Init(512)) {
    1.34 +    fail("Failed to initialize VolatileBuffer");
    1.35 +    return 1;
    1.36 +  }
    1.37 +
    1.38 +  {
    1.39 +    VolatileBufferPtr<char> ptr(heapbuf);
    1.40 +    if (ptr.WasBufferPurged()) {
    1.41 +      fail("Buffer was immediately purged after initialization");
    1.42 +      return 1;
    1.43 +    }
    1.44 +
    1.45 +    if (!ptr) {
    1.46 +      fail("Didn't get a pointer");
    1.47 +      return 1;
    1.48 +    }
    1.49 +  }
    1.50 +
    1.51 +  RefPtr<VolatileBuffer> buf = new VolatileBuffer();
    1.52 +  if (!buf || !buf->Init(16384)) {
    1.53 +    fail("Failed to initialize VolatileBuffer");
    1.54 +    return 1;
    1.55 +  }
    1.56 +
    1.57 +  const char teststr[] = "foobar";
    1.58 +
    1.59 +  {
    1.60 +    VolatileBufferPtr<char> ptr(buf);
    1.61 +    if (ptr.WasBufferPurged()) {
    1.62 +      fail("Buffer should not be purged immediately after initialization");
    1.63 +      return 1;
    1.64 +    }
    1.65 +
    1.66 +    if (!ptr) {
    1.67 +      fail("Didn't get a pointer");
    1.68 +      return 1;
    1.69 +    }
    1.70 +
    1.71 +    {
    1.72 +      VolatileBufferPtr<char> ptr2(buf);
    1.73 +      if (ptr2.WasBufferPurged()) {
    1.74 +        fail("Failed to Lock buffer again while currently locked");
    1.75 +        return 1;
    1.76 +      }
    1.77 +
    1.78 +      if (!ptr2) {
    1.79 +        fail("Didn't get a pointer on the second lock");
    1.80 +        return 1;
    1.81 +      }
    1.82 +
    1.83 +      strcpy(ptr2, teststr);
    1.84 +    }
    1.85 +  }
    1.86 +
    1.87 +  {
    1.88 +    VolatileBufferPtr<char> ptr(buf);
    1.89 +    if (ptr.WasBufferPurged()) {
    1.90 +      fail("Buffer was immediately purged after unlock");
    1.91 +      return 1;
    1.92 +    }
    1.93 +
    1.94 +    if (strcmp(ptr, teststr)) {
    1.95 +      fail("Buffer failed to retain data after unlock");
    1.96 +      return 1;
    1.97 +    }
    1.98 +  }
    1.99 +
   1.100 +  // Test purging if we know how to
   1.101 +#if defined(MOZ_WIDGET_GONK)
   1.102 +  // This also works on Android, but we need root.
   1.103 +  int fd = open("/" ASHMEM_NAME_DEF, O_RDWR);
   1.104 +  if (fd < 0) {
   1.105 +    fail("Failed to open ashmem device");
   1.106 +    return 1;
   1.107 +  }
   1.108 +  if (ioctl(fd, ASHMEM_PURGE_ALL_CACHES, NULL) < 0) {
   1.109 +    fail("Failed to purge ashmem caches");
   1.110 +    return 1;
   1.111 +  }
   1.112 +#elif defined(XP_DARWIN)
   1.113 +  int state;
   1.114 +  vm_purgable_control(mach_task_self(), (vm_address_t)NULL,
   1.115 +                      VM_PURGABLE_PURGE_ALL, &state);
   1.116 +#else
   1.117 +  return 0;
   1.118 +#endif
   1.119 +
   1.120 +  if (!buf->NonHeapSizeOfExcludingThis()) {
   1.121 +    fail("Buffer should not be allocated on heap");
   1.122 +    return 1;
   1.123 +  }
   1.124 +
   1.125 +  {
   1.126 +    VolatileBufferPtr<char> ptr(buf);
   1.127 +    if (!ptr.WasBufferPurged()) {
   1.128 +      fail("Buffer should not be unpurged after forced purge");
   1.129 +      return 1;
   1.130 +    }
   1.131 +
   1.132 +    if (!strcmp(ptr, teststr)) {
   1.133 +      fail("Purge did not actually purge data");
   1.134 +      return 1;
   1.135 +    }
   1.136 +  }
   1.137 +
   1.138 +  {
   1.139 +    VolatileBufferPtr<char> ptr(buf);
   1.140 +    if (ptr.WasBufferPurged()) {
   1.141 +      fail("Buffer still purged after lock");
   1.142 +      return 1;
   1.143 +    }
   1.144 +  }
   1.145 +
   1.146 +  return 0;
   1.147 +}

mercurial