pushpull.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001, 2002 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. * $Id: pushpull.h,v 1.10 2004/11/16 20:36:11 dwmw2 Exp $
  11. *
  12. */
  13. #ifndef __PUSHPULL_H__
  14. #define __PUSHPULL_H__
  15. #include <linux/errno.h>
  16. struct pushpull {
  17. unsigned char *buf;
  18. unsigned int buflen;
  19. unsigned int ofs;
  20. unsigned int reserve;
  21. };
  22. static inline void init_pushpull(struct pushpull *pp, char *buf, unsigned buflen, unsigned ofs, unsigned reserve)
  23. {
  24. pp->buf = buf;
  25. pp->buflen = buflen;
  26. pp->ofs = ofs;
  27. pp->reserve = reserve;
  28. }
  29. static inline int pushbit(struct pushpull *pp, int bit, int use_reserved)
  30. {
  31. if (pp->ofs >= pp->buflen - (use_reserved?0:pp->reserve)) {
  32. return -ENOSPC;
  33. }
  34. if (bit) {
  35. pp->buf[pp->ofs >> 3] |= (1<<(7-(pp->ofs &7)));
  36. }
  37. else {
  38. pp->buf[pp->ofs >> 3] &= ~(1<<(7-(pp->ofs &7)));
  39. }
  40. pp->ofs++;
  41. return 0;
  42. }
  43. static inline int pushedbits(struct pushpull *pp)
  44. {
  45. return pp->ofs;
  46. }
  47. static inline int pullbit(struct pushpull *pp)
  48. {
  49. int bit;
  50. bit = (pp->buf[pp->ofs >> 3] >> (7-(pp->ofs & 7))) & 1;
  51. pp->ofs++;
  52. return bit;
  53. }
  54. static inline int pulledbits(struct pushpull *pp)
  55. {
  56. return pp->ofs;
  57. }
  58. #endif /* __PUSHPULL_H__ */