{"id":24082398,"url":"https://github.com/roen-ro/circularbuffer","last_synced_at":"2025-07-23T16:41:31.029Z","repository":{"id":50351011,"uuid":"150574622","full_name":"Roen-Ro/CircularBuffer","owner":"Roen-Ro","description":" circular buffer, circular queue, cyclic buffer, ring buffer","archived":false,"fork":false,"pushed_at":"2019-03-16T03:53:15.000Z","size":15,"stargazers_count":134,"open_issues_count":0,"forks_count":34,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-30T17:52:24.983Z","etag":null,"topics":["buffer","c","circular-buffer","pointer","pop","push","queue"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Roen-Ro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-27T11:14:02.000Z","updated_at":"2025-02-16T03:23:04.000Z","dependencies_parsed_at":"2022-09-13T07:02:14.113Z","dependency_job_id":null,"html_url":"https://github.com/Roen-Ro/CircularBuffer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Roen-Ro/CircularBuffer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roen-Ro%2FCircularBuffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roen-Ro%2FCircularBuffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roen-Ro%2FCircularBuffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roen-Ro%2FCircularBuffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Roen-Ro","download_url":"https://codeload.github.com/Roen-Ro/CircularBuffer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Roen-Ro%2FCircularBuffer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266715815,"owners_count":23973346,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["buffer","c","circular-buffer","pointer","pop","push","queue"],"created_at":"2025-01-09T23:46:41.141Z","updated_at":"2025-07-23T16:41:30.988Z","avatar_url":"https://github.com/Roen-Ro.png","language":"C","readme":"# CircularBuffer\n\n## Difinition\n\n The difinition of [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer) from Wikipedia:\n\nA circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.\n\nA circular buffer first starts empty and of some predefined length. For example, this is a 7-element buffer:\n\n![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/f7/Circular_buffer_-_empty.svg/500px-Circular_buffer_-_empty.svg.png)\n\nAssume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):\n\n![](https://upload.wikimedia.org/wikipedia/commons/thumb/8/89/Circular_buffer_-_XX1XXXX.svg/500px-Circular_buffer_-_XX1XXXX.svg.png)\n\nThen assume that two more elements are added — 2 \u0026 3 — which get appended after the 1:\n\n![](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Circular_buffer_-_XX123XX.svg/500px-Circular_buffer_-_XX123XX.svg.png)\n\nIf two elements are then removed from the buffer, the oldest values inside the buffer are removed. The two elements removed, in this case, are 1 \u0026 2, leaving the buffer with just a 3:\n\n![](https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Circular_buffer_-_XXXX3XX.svg/500px-Circular_buffer_-_XXXX3XX.svg.png)\n\nIf the buffer has 7 elements then it is completely full:\n\n![](https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/Circular_buffer_-_6789345.svg/500px-Circular_buffer_-_6789345.svg.png)\n\nA consequence of the circular buffer is that when it is full and a subsequent write is performed, then it starts overwriting the oldest data. In this case, two more elements — A \u0026 B — are added and they overwrite the 3 \u0026 4:\n\n![](https://upload.wikimedia.org/wikipedia/commons/thumb/b/ba/Circular_buffer_-_6789AB5.svg/500px-Circular_buffer_-_6789AB5.svg.png)\n\nAlternatively, the routines that manage the buffer could prevent overwriting the data and return an error or raise an exception. Whether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular buffer.\n\nFinally, if two elements are now removed then what would be returned is not 3 \u0026 4 but 5 \u0026 6 because A \u0026 B overwrote the 3 \u0026 the 4 yielding the buffer with:\n\n![](https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Circular_buffer_-_X789ABX.svg/500px-Circular_buffer_-_X789ABX.svg.png)\n\n## Interfaces\n``` C\ntypedef struct s_circularBuffer* CircularBuffer;\n\n// Construct CircularBuffer with ‘size' in byte. You must call CircularBufferFree() in balance for destruction.\nextern CircularBuffer CircularBufferCreate(size_t size);\n\n// Destruct CircularBuffer \nextern void CircularBufferFree(CircularBuffer cBuf);\n\n// Reset the CircularBuffer\nextern void CircularBufferReset(CircularBuffer cBuf);\n\n// get the buffer capacity\nextern size_t CircularBufferGetCapacity(CircularBuffer cBuf);\n\n//get occupied data size of CircularBuffer\nextern size_t CircularBufferGetDataSize(CircularBuffer cBuf);\n\n// Push data to the tail of a circular buffer from 'src' with 'length' size in byte.\nextern void CircularBufferPush(CircularBuffer cBuf,void *src, size_t length);\n\n// Pop data from a circular buffer to 'dataOut'  with wished 'length' size in byte,return the actual data size in byte popped out,which is less or equal to the input 'length parameter.\nextern size_t CircularBufferPop(CircularBuffer cBuf, size_t length, void *dataOut);\n\n// Read data from a circular buffer to 'dataOut'  with wished 'length' size in byte,return the actual data size in byte popped out,which is less or equal to the input 'length parameter.\nextern size_t CircularBufferRead(CircularBuffer cBuf, size_t length, void *dataOut);\n\n//for test purpose, print the circular buffer's data content by printf(...); the 'hex' parameters indicates that if the data should be printed in asscii string or hex data format.\nextern void CircularBufferPrint(CircularBuffer cBuf, bool hex);\n\n```\n\n\n## Author\n\nRoen(罗亮富), zxllf23@163.com\n\n## Licenses\n\nAll source code is licensed under the MIT License\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froen-ro%2Fcircularbuffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froen-ro%2Fcircularbuffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froen-ro%2Fcircularbuffer/lists"}