C 实现智能指针

  • C++11版本开始支持智能指针的原理其实就是引用计数,C并不原生支持,但可以通过一些方法模拟出来,下面是参考 libre 的实现,可以使用 Valgrind 调试内存的使用状态,不废话,用代码解释代码:
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

typedef void (mem_destroy_h)(void *data);

/** Defines a reference-counting memory object */
struct mem {
    uint32_t nrefs;     /**< Number of references  */
    mem_destroy_h *dh;  /**< Destroy handler       */
};

/**
 * Allocate a new reference-counted memory object
 *
 * @param size Size of memory object
 * @param dh   Optional destructor, called when destroyed
 *
 * @return Pointer to allocated object
 */
void *mem_alloc(size_t size, mem_destroy_h *dh)
{
    struct mem *m;

    m = malloc(sizeof(*m) + size);
    if (!m)
        return NULL;

    m->nrefs = 1;
    m->dh    = dh;

    return (void *)(m + 1);
}

/**
 * Reference a reference-counted memory object
 *
 * @param data Memory object
 *
 * @return Memory object (same as data)
 */
void *mem_ref(void *data)
{
    struct mem *m;

    if (!data)
        return NULL;

    m = ((struct mem *)data) - 1;

    ++m->nrefs;

    return data;
}

/**
 * Dereference a reference-counted memory object. When the reference count
 * is zero, the destroy handler will be called (if present) and the memory
 * will be freed
 *
 * @param data Memory object
 *
 * @return Always NULL
 */
void *mem_deref(void *data)
{
    struct mem *m;

    if (!data)
        return NULL;

    m = ((struct mem *)data) - 1;

    if (--m->nrefs > 0)
        return NULL;

    if (m->dh)
        m->dh(data);

    /* NOTE: check if the destructor called mem_ref() */
    if (m->nrefs > 0)
        return NULL;

    free(m);

    return NULL;
}

int main(int argc, const char * argv[])
{
    void * data_foo = mem_alloc(512, NULL);
    data_foo = mem_deref(data_foo);
    return 0;
}

参考文档:

  1. https://github.com/creytiv/re/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值