linux-bk/lib/kref.c
<<
>>
Prefs
   1/*
   2 * kref.c - library routines for handling generic reference counted objects
   3 *
   4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
   5 * Copyright (C) 2004 IBM Corp.
   6 *
   7 * based on lib/kobject.c which was:
   8 * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
   9 *
  10 * This file is released under the GPLv2.
  11 *
  12 */
  13
  14/* #define DEBUG */
  15
  16#include <linux/kref.h>
  17#include <linux/module.h>
  18
  19/**
  20 * kref_init - initialize object.
  21 * @kref: object in question.
  22 * @release: pointer to a function that will clean up the object
  23 *           when the last reference to the object is released.
  24 *           This pointer is required.
  25 */
  26void kref_init(struct kref *kref, void (*release)(struct kref *kref))
  27{
  28        WARN_ON(release == NULL);
  29        atomic_set(&kref->refcount,1);
  30        kref->release = release;
  31}
  32
  33/**
  34 * kref_get - increment refcount for object.
  35 * @kref: object.
  36 */
  37struct kref *kref_get(struct kref *kref)
  38{
  39        WARN_ON(!atomic_read(&kref->refcount));
  40        atomic_inc(&kref->refcount);
  41        return kref;
  42}
  43
  44/**
  45 * kref_put - decrement refcount for object.
  46 * @kref: object.
  47 *
  48 * Decrement the refcount, and if 0, call kref->release().
  49 */
  50void kref_put(struct kref *kref)
  51{
  52        if (atomic_dec_and_test(&kref->refcount)) {
  53                pr_debug("kref cleaning up\n");
  54                kref->release(kref);
  55        }
  56}
  57
  58EXPORT_SYMBOL(kref_init);
  59EXPORT_SYMBOL(kref_get);
  60EXPORT_SYMBOL(kref_put);
  61
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.