midpresourcelimit.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 596 行 · 第 1/2 页
C
596 行
/* * * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//** * @file midpResourceLimit.c * MIDP Resource limit * * MIDP native resource limit implementation. * * @note * * @header midpResourceLimit.h */#include <midpServices.h>#include <midpResourceLimit.h>#include <midpMidletSuiteUtils.h>#include <jvm.h>#include <midp_logging.h>#include <midpMalloc.h>#include <midp_properties_port.h>#include <stdio.h>#if 0 /* for local debug */#define REPORT_INFO(a,b) printf(b)#define REPORT_INFO1(a,b,c) printf(b,c)#define REPORT_INFO2(a,b,c,d) printf(b,c,d)#define REPORT_INFO3(a,b,c,d,e) printf(b,c,d,e)#define REPORT_WARN3(a,b,c,d,e) printf(b,c,d,e)#endif#if 0#define MEASURE_HIGHWATER_MARK#endif/** * Macros for easy access to Resource Table values */#define IS_AMS_ISOLATE (getCurrentIsolateId()==midpGetAmsIsolateId())#define GLOBAL_LIMIT(type) gGlobalResourceTable[0][(type)]#define SUITE_RESERVED(type) gGlobalResourceTable[IS_AMS_ISOLATE?1:3][(type)]#define SUITE_LIMIT(type) gGlobalResourceTable[IS_AMS_ISOLATE?2:4][(type)]#define NON_AMS_RESERVED(type) gGlobalResourceTable[3][(type)]/** * Resource Limit Table (R/O) This table is statically initialised for * global and per suite limits for all resource types. */const int gGlobalResourceTable[5][RSC_TYPE_COUNT] = { { TCP_CLI_GLOBAL_LIMIT, /* RSC_TYPE_TCP_CLI*/ TCP_SER_GLOBAL_LIMIT, /* RSC_TYPE_TCP_SER*/ UDP_GLOBAL_LIMIT, /* RSC_TYPE_UDP*/ FILE_GLOBAL_LIMIT, /* RSC_TYPE_FILE*/ AUDIO_CHA_GLOBAL_LIMIT, /* RSC_TYPE_AUDIO_CHA*/ IMAGE_MUT_GLOBAL_LIMIT, /* RSC_TYPE_IMAGE_MUT*/ IMAGE_IMMUT_GLOBAL_LIMIT /* RSC_TYPE_IMAGE_IMMUT*/ }, { TCP_CLI_AMS_RESERVED, /* RSC_TYPE_TCP_CLI*/ TCP_SER_AMS_RESERVED, /* RSC_TYPE_TCP_SER*/ UDP_AMS_RESERVED, /* RSC_TYPE_UDP*/ FILE_AMS_RESERVED, /* RSC_TYPE_FILE*/ AUDIO_CHA_AMS_RESERVED, /* RSC_TYPE_AUDIO_CHA*/ IMAGE_MUT_AMS_RESERVED, /* RSC_TYPE_IMAGE_MUT*/ IMAGE_IMMUT_AMS_RESERVED /* RSC_TYPE_IMAGE_IMMUT*/ }, { TCP_CLI_AMS_LIMIT, /* RSC_TYPE_TCP_CLI*/ TCP_SER_AMS_LIMIT, /* RSC_TYPE_TCP_SER*/ UDP_AMS_LIMIT, /* RSC_TYPE_UDP*/ FILE_AMS_LIMIT, /* RSC_TYPE_FILE*/ AUDIO_CHA_AMS_LIMIT, /* RSC_TYPE_AUDIO_CHA*/ IMAGE_MUT_AMS_LIMIT, /* RSC_TYPE_IMAGE_MUT*/ IMAGE_IMMUT_AMS_LIMIT /* RSC_TYPE_IMAGE_IMMUT*/ }, { TCP_CLI_SUITE_RESERVED, /* RSC_TYPE_TCP_CLI*/ TCP_SER_SUITE_RESERVED, /* RSC_TYPE_TCP_SER*/ UDP_SUITE_RESERVED, /* RSC_TYPE_UDP*/ FILE_SUITE_RESERVED, /* RSC_TYPE_FILE*/ AUDIO_CHA_SUITE_RESERVED, /* RSC_TYPE_AUDIO_CHA*/ IMAGE_MUT_SUITE_RESERVED, /* RSC_TYPE_IMAGE_MUT*/ IMAGE_IMMUT_SUITE_RESERVED /* RSC_TYPE_IMAGE_IMMUT*/ }, { TCP_CLI_SUITE_LIMIT, /* RSC_TYPE_TCP_CLI*/ TCP_SER_SUITE_LIMIT, /* RSC_TYPE_TCP_SER*/ UDP_SUITE_LIMIT, /* RSC_TYPE_UDP*/ FILE_SUITE_LIMIT, /* RSC_TYPE_FILE*/ AUDIO_CHA_SUITE_LIMIT, /* RSC_TYPE_AUDIO_CHA*/ IMAGE_MUT_SUITE_LIMIT, /* RSC_TYPE_IMAGE_MUT*/ IMAGE_IMMUT_SUITE_LIMIT /*RSC_TYPE_IMAGE_IMMUT*/ }};/** * Structure for isolate resource usage tracking */typedef struct { int isolateId; int inUse; int resourceUsage[RSC_TYPE_COUNT];#ifdef MEASURE_HIGHWATER_MARK int resourceMaxUsage[RSC_TYPE_COUNT];#endif} _IsolateResourceUsage;/** * Resource available table (R/W) */static int gResourcesAvailable[RSC_TYPE_COUNT] = { TCP_CLI_GLOBAL_LIMIT - TCP_CLI_AMS_RESERVED, /* RSC_TYPE_TCP_CLI*/ TCP_SER_GLOBAL_LIMIT - TCP_SER_AMS_RESERVED, /* RSC_TYPE_TCP_SER*/ UDP_GLOBAL_LIMIT - UDP_AMS_RESERVED, /* RSC_TYPE_UDP*/ FILE_GLOBAL_LIMIT - FILE_AMS_RESERVED, /*RSC_TYPE_FILE*/ AUDIO_CHA_GLOBAL_LIMIT - AUDIO_CHA_AMS_RESERVED, /* RSC_TYPE_AUDIO_CHA*/ IMAGE_MUT_GLOBAL_LIMIT - IMAGE_MUT_AMS_RESERVED, /* RSC_TYPE_IMAGE_MUT*/ IMAGE_IMMUT_GLOBAL_LIMIT - IMAGE_IMMUT_AMS_RESERVED /*RSC_TYPE_IMAGE_IMMUT*/};static int isInitialized = KNI_FALSE;static _IsolateResourceUsage* gIsolateResourceUsage;static int max_isolates = 0;/** * Initialize the Resource limit structures. */static void initResourceLimit(void) { int i, j;#if ENABLE_CDC /* CDC does not have isolates. */ max_isolates = 1;#else#if ENABLE_MULTIPLE_ISOLATES max_isolates = getInternalPropertyInt("MAX_ISOLATES"); if (0 == max_isolates) { char max_isolates_str[5]; REPORT_INFO(LC_AMS, "MAX_ISOLATES property not set"); /* set XML constant value as property value */ max_isolates = MAX_ISOLATES; sprintf(max_isolates_str, "%d", max_isolates); setInternalProperty("MAX_ISOLATES", max_isolates_str); }#else max_isolates = 1; setInternalProperty("MAX_ISOLATES", "1");#endif#endif REPORT_INFO(LC_CORE, "initialize resource limit\n"); gIsolateResourceUsage = (_IsolateResourceUsage*) midpMalloc(sizeof(_IsolateResourceUsage) * max_isolates); for (i = 0; i < max_isolates; i++) { gIsolateResourceUsage[i].isolateId = -1; gIsolateResourceUsage[i].inUse = 0; for (j = 0; j < RSC_TYPE_COUNT; j++) { gIsolateResourceUsage[i].resourceUsage[j] = 0;#ifdef MEASURE_HIGHWATER_MARK gIsolateResourceUsage[i].resourceMaxUsage[j] = 0;#endif } } /* initialize the entry for the AMS */ gIsolateResourceUsage[0].isolateId = 1; gIsolateResourceUsage[0].inUse = 1; isInitialized = KNI_TRUE;}/** * Finalize the Resource limit structures. */void midpFinalizeResourceLimit(void) { if (isInitialized) { if (gIsolateResourceUsage) { midpFree(gIsolateResourceUsage); gIsolateResourceUsage = NULL; } isInitialized = KNI_FALSE; }}/** * Find the _IsolateResourceUsage structure for the given isolateId. * * @param isolateId id of the isolate * * @return the _IsolateResourceUsage structure for the given isolateId if it * exist, otherwise 0 */static _IsolateResourceUsage *findIsolateResourceUsageStruct(int isolateId) { int i; if (!isInitialized) { initResourceLimit(); } /* the first entry is the ams */ if (isolateId == midpGetAmsIsolateId()) { return &(gIsolateResourceUsage[0]); } for (i = 0; i < max_isolates; i++) { if (isolateId == gIsolateResourceUsage[i].isolateId) { return &(gIsolateResourceUsage[i]); } } REPORT_INFO1(LC_CORE, "RESOURCES [%d] isolateId not in resource table\n", isolateId); return 0;}/** * Verify that the resource limit is not crossed. * * @param entry pointer to isolate's _IsolateResourceUsage structure * @param type Resource type * @param requestSize Requesting size * * @return 1 if resource limit is not crossed, otherwise 0 */static int checkResourceLimit(_IsolateResourceUsage *entry, RscType type, int requestSize) { if (entry->resourceUsage[type] + requestSize <= SUITE_LIMIT(type)) { int fromGlobal = requestSize; if (entry->resourceUsage[type] < SUITE_RESERVED(type)) { /* part or all of the needed resource is already reserved */ fromGlobal = (entry->resourceUsage[type] + requestSize) - SUITE_RESERVED(type); if (fromGlobal < 0) { fromGlobal = 0; } } if (gResourcesAvailable[type] - fromGlobal >= 0) { return 1; } } REPORT_INFO3(LC_CORE, "RESOURCES [%d] checkResourceLimit FAILED" \ " used=%d global=%d\n", entry->isolateId, entry->resourceUsage[type], gResourcesAvailable[type]); return 0;}/** * Verify that the resource limit is not crossed. IsolateID will be * fetched from getCurrentIsolateId() as defined in midpServices.h * * @param type Resource type * @param requestSize Requesting size * * @return 1 if resource limit is not crossed, otherwise 0 */int midpCheckResourceLimit(RscType type, int requestSize) { int isolateId = getCurrentIsolateId(); _IsolateResourceUsage *entry = findIsolateResourceUsageStruct(isolateId); REPORT_INFO3(LC_CORE, "RESOURCES [%d] midpCheckResourceLimit(%d, %d)\n",
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?