1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/memory/mozalloc/mozalloc_oom.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,57 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: sw=4 ts=4 et : 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#if defined(XP_WIN) 1.12 +# define MOZALLOC_EXPORT __declspec(dllexport) 1.13 +#endif 1.14 + 1.15 +#include "mozilla/mozalloc_abort.h" 1.16 +#include "mozilla/mozalloc_oom.h" 1.17 +#include "mozilla/Assertions.h" 1.18 + 1.19 +static mozalloc_oom_abort_handler gAbortHandler; 1.20 + 1.21 +#define OOM_MSG_LEADER "out of memory: 0x" 1.22 +#define OOM_MSG_DIGITS "0000000000000000" // large enough for 2^64 1.23 +#define OOM_MSG_TRAILER " bytes requested" 1.24 +#define OOM_MSG_FIRST_DIGIT_OFFSET sizeof(OOM_MSG_LEADER) - 1 1.25 +#define OOM_MSG_LAST_DIGIT_OFFSET sizeof(OOM_MSG_LEADER) + \ 1.26 + sizeof(OOM_MSG_DIGITS) - 3 1.27 + 1.28 +static const char *hex = "0123456789ABCDEF"; 1.29 + 1.30 +void 1.31 +mozalloc_handle_oom(size_t size) 1.32 +{ 1.33 + char oomMsg[] = OOM_MSG_LEADER OOM_MSG_DIGITS OOM_MSG_TRAILER; 1.34 + size_t i; 1.35 + 1.36 + // NB: this is handle_oom() stage 1, which simply aborts on OOM. 1.37 + // we might proceed to a stage 2 in which an attempt is made to 1.38 + // reclaim memory 1.39 + 1.40 + if (gAbortHandler) 1.41 + gAbortHandler(size); 1.42 + 1.43 + static_assert(OOM_MSG_FIRST_DIGIT_OFFSET > 0, 1.44 + "Loop below will never terminate (i can't go below 0)"); 1.45 + 1.46 + // Insert size into the diagnostic message using only primitive operations 1.47 + for (i = OOM_MSG_LAST_DIGIT_OFFSET; 1.48 + size && i >= OOM_MSG_FIRST_DIGIT_OFFSET; i--) { 1.49 + oomMsg[i] = hex[size % 16]; 1.50 + size /= 16; 1.51 + } 1.52 + 1.53 + mozalloc_abort(oomMsg); 1.54 +} 1.55 + 1.56 +void 1.57 +mozalloc_set_oom_abort_handler(mozalloc_oom_abort_handler handler) 1.58 +{ 1.59 + gAbortHandler = handler; 1.60 +}