zoukankan      html  css  js  c++  java
  • A Reusable Aspect for Memory Allocation Checking

    The checking logic would be refactored into an aspect file, as follows:

    after(void * s) : (call($ malloc(...)) || call($ calloc(...)) || call($ realloc(...))) 
    			&& result(s) {
    	char * result = (char *)(s);
    	if (result == NULL) {
    		/* routine to handle the case when memory allocation fails */
    	}
    } 
    
    


    Now, the core program looks as follows:

    ...
    int *x ;
    x = (int *)malloc(sizeof(int) * 4);		<--- dynamic memory allocation
    /* routine for handling the normal case */
    
    ...

    例子:
    文件:mal.acc

    #include <stdio.h>
    after(void * s) : (call($ malloc(...)) || call($ calloc(...)) || call($ realloc(...)))
    && result(s) {
    char * result = (char *)(s);
    if (result != NULL) {
    printf(" aspectC:Memory Allocation Success ! ");
    }
    else{
    printf(" aspectC:Memory Allocation Faile ! ");
    }
    }

    文件mal.c:

    #include <stdio.h>
    #include <malloc.h>
    void t1()
    {
    int *x ;
    printf(" core code:hehe ! ");
    x = (int *)malloc(sizeof(int) * 4);
    printf(" core code:hehe ! ! ");
    }
    int main()
    {
    t1();
    int *x ;
    printf(" core code:hehe ! ! ");
    x = (int *)malloc(sizeof(int) * 4);
    printf(" core code:hehe ! ! ");
    return 0;
    }

  • 相关阅读:
    进程与线程的区别
    信号列表详解
    同步与互斥
    互斥锁
    读写锁
    Redis QPS测试
    从分布式锁来看redis和zookpeer!
    JVM虚拟机调参
    log4j.properties配置详解与实例
    生产者消费者(消费者要消费完才能退出)
  • 原文地址:https://www.cnblogs.com/xiaohuihui123/p/4563753.html
Copyright © 2011-2022 走看看