Code Monkey home page Code Monkey logo

soft-where-inc / lineofcode Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 105 KB

Machinery to uniquely generate __LOC__, a 4-byte int encoding of __FILE__, __LINE__ & __FUNC__. 'C' interfaces are provided to generate the encoding at compile-time and to decode the encoded value to its constituent file/func-name and line-number. Encoded line-of-code can be used for diagnostics, troubleshooting, resource tracking and other usages.

License: Apache License 2.0

Python 50.25% C 31.87% Makefile 12.22% Shell 2.20% C++ 3.45%
diagnostics python softwareengineering tooling c cpp resource-monitor tracing

lineofcode's Introduction

LOC - Line Of Code, aka Code Location

Frequently for instrumentation, tracing and diagnostics, one needs to track the code-location of where certain things occur. An example is to track the place where memory allocation is done, or some other resource such as a lock request is obtained.

LOC is a technique to encode the line-of-code, i.e., the file name and line number pair, as an extremely compact 4-byte integer. (The line-of-code is also referred to as the code-location or call-site.)

The LOC-encoded integer can then be stored in your core structures, passed-around the stack, and generally manipulated as an opaque integer.

This repository contains a Python script that generates core LOC interface .h files and basic .c files which you can then integrate with your C/C++ code-base.

The generated LOC header files provide encoding macros to generate the 4-byte encoded line-of-code (i.e, code-location) and decoding macros to unpack the encoded integer value into its consitituent file-name and line-number.

Sample changes to Makefile are provided to show how to integrate the generated files into any typical C/C++ code base. Example programs are provided to demonstrate how to use this LOC encoding to drive diagnostcs & instrumentation with very little overhead of space consumed at run-time.

Refer to the Workflow document for more details.


Alternate Solutions

Classical techniques that exist to track file-name / line-number pair usually require a const char * file pointer for the __FILE__ macro, consuming 8-bytes and a 4-byte line number, given by the __LINE__ macro. Passing this around on the stack requires at least 12 bytes.

Morover, if you wish to store this pair in some common (diagnostic) structure, you will further need to allocate space for the file-name itself. Storing, say, a minimum of 8-characters for a file name plus line-number requires 12 bytes, which can be reduced to, say, 10 bytes by storing the line number as uint16_t. It is not uncommon to save-off 12 to 14 chars of file-name, plus 4 to 2 bytes, respectively, of line-number, requiring a total of 16 bytes.

lineofcode's People

Contributors

gapisback avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.