Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1 KiB

  1. #include "stdafx.h"
  2. #include "SharedMemory.h"
  3. int CircBuffWrite(BYTE *pbStart, int cbBuff, int obDst, BYTE *pbSrc, int cb)
  4. {
  5. // Assumes cb bytes are available and 0 <= obDst <= cbBuff.
  6. // cb1 is the lesser of number of bytes before the end of the buffer
  7. // and the number of bytes to write.
  8. int cb1 = min(cb, cbBuff - obDst);
  9. if (cb1 > 0)
  10. {
  11. memcpy(pbStart + obDst, pbSrc, cb1);
  12. obDst += cb1;
  13. cb -= cb1;
  14. }
  15. // If there's anything left, then we wrapped around to the
  16. // beginning of the buffer.
  17. if (cb > 0)
  18. {
  19. memcpy(pbStart, pbSrc + cb1, cb);
  20. obDst = cb1;
  21. }
  22. return obDst;
  23. }
  24. int CircBuffRead(BYTE *pbStart, int cbBuff, int obSrc, BYTE *pbDst, int cb)
  25. {
  26. // Assumes cb bytes are available and 0 <= obDst <= cbBuff.
  27. // cb1 is the lesser of number of bytes before the end of the buffer
  28. // and the number of bytes to write.
  29. int cb1 = min(cb, cbBuff - (pbDst - pbStart));
  30. if (cb1 > 0)
  31. {
  32. memcpy(pbDst, pbStart + obSrc, cb1);
  33. obSrc += cb1;
  34. cb -= cb1;
  35. }
  36. // If there's anything left, then we wrapped around to the
  37. // beginning of the buffer.
  38. if (cb > 0)
  39. {
  40. memcpy(pbDst + cb1, pbStart, cb);
  41. obSrc = cb1;
  42. }
  43. return obSrc;
  44. }