Esempio n. 1
0
void GuardingAllocator::deallocate(void *address)
{
    if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(0 == address)) {
        BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
        return;                                                       // RETURN
    }

    const int pageSize = getSystemPageSize();

    void *firstPage;  // address of the first page of the allocation
    void *guardPage;  // address of the guard page

    if (e_BEFORE_USER_BLOCK == d_guardPageLocation) {
        // The memory page before the block returned to the user is protected.

        firstPage = static_cast<char *>(address) - pageSize;
        guardPage = firstPage;
    }
    else {
        // The memory page after the block returned to the user is protected.

        firstPage = *(void **)(static_cast<char *>(address) - OFFSET);
        guardPage = *(void **)(static_cast<char *>(address) - OFFSET * 2);
    }

    // Unprotect the guard page and free the memory.

    const int rc = systemUnprotect(guardPage, pageSize);
    (void)rc;

    BSLS_ASSERT_OPT(0 == rc);

    systemFree(firstPage);
}
Esempio n. 2
0
// MANIPULATORS
void *GuardingAllocator::allocate(size_type size)
{
    if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(0 == size)) {
        BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
        return 0;                                                     // RETURN
    }

    const size_type paddedSize =
                          bsls::AlignmentUtil::roundUpToMaximalAlignment(size);

    // Adjust for additional memory needed to stash reference addresses when
    // 'e_AFTER_USER_BLOCK' is in use.

    const int adjustedSize = e_AFTER_USER_BLOCK == d_guardPageLocation
                             ? paddedSize + OFFSET * 2
                             : paddedSize;

    // Calculate the number of pages to allocate, *not* counting the guard
    // page.

    const int pageSize = getSystemPageSize();
    const int numPages = (adjustedSize + pageSize - 1) / pageSize;

    const size_type totalSize = (numPages + 1) * pageSize;  // add 1 for guard

    void *firstPage = systemAlloc(totalSize);

    if (!firstPage) {
#ifdef BDE_BUILD_TARGET_EXC
        BSLS_THROW(bsl::bad_alloc());
#else
        return 0;                                                     // RETURN
#endif
    }

    void *userAddress;  // address to return to the caller
    void *guardPage;    // address of the guard page for this allocation

    if (e_BEFORE_USER_BLOCK == d_guardPageLocation) {
        // Protect the memory page before the block returned to the user.

        guardPage   = firstPage;
        userAddress = static_cast<char *>(firstPage) + pageSize;
    }
    else {
        // Protect the memory page after the block returned to the user.

        guardPage   = static_cast<char *>(firstPage) + (numPages * pageSize);
        userAddress = static_cast<char *>(guardPage) - paddedSize;

        // Stash the reference addresses required by 'deallocate'.

        *(void **)(static_cast<char *>(userAddress) - OFFSET)     = firstPage;
        *(void **)(static_cast<char *>(userAddress) - OFFSET * 2) = guardPage;
    }

    // Protect the guard page from read/write access.

    if (0 != systemProtect(guardPage, pageSize)) {
        systemFree(firstPage);
#ifdef BDE_BUILD_TARGET_EXC
        BSLS_THROW(bsl::bad_alloc());
#else
        return 0;                                                     // RETURN
#endif
    }

    return userAddress;
}
Esempio n. 3
0
File: gjc.c Progetto: hgia1234/a2
int main(int argc, char* argv[])
{
    GJCType gjc;
    int initFlag, dataFlag, exitFlag=FALSE;
    char c[MAX_OPTION_INPUT+EXTRA_SPACES];
    /* Initialise the Gloria Jean's Coffee system to a safe empty state. */
    initFlag = systemInit(&gjc);

    /* Populate the Gloria Jean's Coffee system with data from the data files. */
    /* Uncomment this line when you are ready to use command line arguments:*/
    if(argv[1]==NULL||argv[2]==NULL){
	printf("No argument found\n");
	exit(EXIT_SUCCESS);
    }


    dataFlag = loadData(&gjc, argv[1], argv[2]); 

    /* Testing to see if both systemInit(.) and loadData(.) are ok */
    if (initFlag == FAILURE || dataFlag == FAILURE){
	exit(EXIT_FAILURE);
    }


    /* Interactive menu providing user with access to the 9 menu options */
    while(exitFlag==FALSE){
	printf("Main Menu:\n");


	printf("(1) Hot Drinks Summary\n");
	printf("(2) Cold Drinks Summary\n");
	printf("(3) Detailed Menu Report\n");
	printf("(4) Add Menu Category\n");
	printf("(5) Delete Menu Category\n");
	printf("(6) Add Menu Item\n");
	printf("(7) Delete Menu Item\n");
	printf("(8) Save & Exit\n");
	printf("(9) Abort\n");
	printf("Select your option (1-9):");

	fgets(c,MAX_OPTION_INPUT+EXTRA_SPACES,stdin);
	if(c[0]=='\n'){
	    printf("Invalid input\n");
	}else if(stringIsInRange(c,MAX_OPTION_INPUT)==FALSE){
	    readRestOfLine();
	    printf("Invalid input\n");
	}else{
	    stripNewLine(c);
	    if(strcmp(c,"1")==0){
		displaySummary(&gjc,HOT);	        
	    }else if(strcmp(c,"2")==0){
		displaySummary(&gjc,COLD);	        
	    }else if(strcmp(c,"3")==0){
	    }else if(strcmp(c,"4")==0){
	    }else if(strcmp(c,"5")==0){
	    }else if(strcmp(c,"6")==0){
	    }else if(strcmp(c,"7")==0){
	    }else if(strcmp(c,"8")==0){
	    }else if(strcmp(c,"9")==0){
		exitFlag=TRUE;
	    } 
	}
    }

    /* Deallocate all dynamically allocated memory. */
    systemFree(&gjc);

    exit(EXIT_SUCCESS);

}