rdiscount_wm 3.0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 08d12417996bd80360088e8ee353c5c48f13eb6f
4
+ data.tar.gz: 8143aa7de8d229607aeb59236bc99a8d4cdb6eaa
5
+ SHA512:
6
+ metadata.gz: 5ceaa8abdb29d93cb006e472ec5ffa8736e27c9938692711e9bc8807573f2a03fe00d52833e6b120ee133d2c1ac853f2f4fee777b7abe5946f1ddf11d7a217bc
7
+ data.tar.gz: 30ce6b3386f3dddebf5c91acccd5b16dbce1404e0a83cd05ccd3528e0ee16ba65aa2031e24ccbe772f591dab2d532b6579d70a9889f3edfc89238c337a35120e
data/BUILDING ADDED
@@ -0,0 +1,148 @@
1
+ BUILDING rdiscount
2
+ ==================
3
+
4
+ You'll be needing Ruby, rake, and a basic build environment.
5
+
6
+ Build the rdiscount extension for tests and local development:
7
+
8
+ $ rake build
9
+
10
+ Use your rdiscount working copy when running ruby programs:
11
+
12
+ $ export RUBYLIB=~/rdiscount/lib:$RUBYLIB
13
+ $ ruby some-program.rb
14
+
15
+ Gathering changes from an upstream discount clone requires first
16
+ grabbing the discount submodule into the root of the project and then running
17
+ the rake gather task to copy discount source files into the ext/ directory:
18
+
19
+ $ git submodule update --init
20
+ Submodule 'discount' (git://github.com/davidfstr/discount.git) registered for path 'discount'
21
+ Cloning into discount...
22
+ $ cd discount
23
+ $ ./configure.sh --with-fenced-code --with-github-tags --with-dl=both
24
+ $ make # ensure it compiles
25
+ $ cd ..
26
+ $ rake gather
27
+ $ rake build
28
+
29
+
30
+ UPGRADING Discount
31
+ ==================
32
+
33
+ The most common maintenance task is upgrading the version of Discount that
34
+ RDiscount is using.
35
+
36
+ Before doing anything, make sure you can build the current (unmodified) version
37
+ of RDiscount. See the section above for details.
38
+
39
+ Update the Discount submodule to the desired version:
40
+
41
+ $ cd discount
42
+ $ git fetch
43
+ $ git checkout v2.0.7.x # insert desired version
44
+ $ cd ..
45
+
46
+ Copy the new Discount sources to the appropriate directories for RDiscount:
47
+
48
+ $ rake gather
49
+
50
+ Update rdiscount.gemspec to include all *.c, *.h, and *.rb files in
51
+ ext. This must be done manually. Here's a quick way to get the full list:
52
+
53
+ $ echo ext/*.c ext/*.h ext/*.rb ext/blocktags ext/VERSION | tr ' ' "\n" | sort
54
+
55
+ Build the RDiscount gem. If you get errors related to missing files
56
+ in ext, make sure you updated the gemspec correctly in the previous step.
57
+
58
+ $ gem build rdiscount.gemspec
59
+
60
+ Install this new gem locally. It is recommended that you use RVM to
61
+ create an isolated installation environment. If you get an error after the line
62
+ "Building native extensions", see the troubleshooting section below.
63
+
64
+ $ rvm ruby@rdiscount --create # recommended; requires RVM
65
+ $ gem install rdiscount-*.gem
66
+
67
+ Make sure the gem can be imported:
68
+
69
+ $ ruby -e 'require "rdiscount"'
70
+
71
+ Make sure the tests (still) pass:
72
+
73
+ $ rake test
74
+
75
+ Worked? Swell! The hard part is past.
76
+
77
+ Check the Discount release notes to determine whether it has gained any new
78
+ features that should be exposed through the RDiscount Ruby interface
79
+ (lib/rdiscount.rb), such as new MKD_* flags or configure flags.
80
+ If so, update the Ruby interface.
81
+
82
+ If the ./configure.sh line needs to be changed to support new features,
83
+ you will need to port some #defines from discount/config.h to ext/config.h
84
+ manually to get RDiscount's embedded Discount to use the same configure flags.
85
+
86
+ For new Discount extensions via new MKD_* flags, you will need to update:
87
+
88
+ * lib/rdiscount.rb with new accessors and
89
+ * the rb_rdiscount__get_flags function in ext/rdiscount.c with new
90
+ accessor-to-flag mappings for each new extension.
91
+
92
+ You should also look for RDiscount-specific bugs & feature requests in the
93
+ GitHub tracker and fix a few.
94
+
95
+ If any bugs were fixed or features added be sure to also add new tests!
96
+ And don't forget to rerun the preexisting tests.
97
+
98
+ Update the CHANGELOG.
99
+
100
+ Update rdiscount.gemspec with the new RDiscount version number and date.
101
+ Also update the VERSION constant in lib/rdiscount.rb.
102
+ Push that change as the final commit with a message in the format
103
+ "2.0.7 release".
104
+
105
+ Tag the release commit:
106
+
107
+ $ git tag 2.0.7 # insert desired version
108
+
109
+ Rebuild the gem file and push it to RubyGems.
110
+
111
+ $ gem build rdiscount.gemspec
112
+ $ gem push rdiscount-NEW_VERSION.gem
113
+
114
+ Announce the new release! For releases with new features it is recommended to
115
+ write a full blog post.
116
+
117
+
118
+ Troubleshooting Native Extension Issues
119
+ ---------------------------------------
120
+
121
+ The most likely place where errors will crop up is when you attempt to build
122
+ the C extension. If this happens, you will have to debug manually. Below are
123
+ a few recommended sanity checks:
124
+
125
+ Ensure the Makefile is generated correctly:
126
+
127
+ $ cd ext
128
+ $ ruby extconf.rb
129
+
130
+ Ensure make succeeds:
131
+
132
+ $ make
133
+
134
+ If you get linker errors related to there being duplicate symbols for _main,
135
+ you probably need to update the deploy target of the Rakefile to exclude
136
+ new *.c files from Discount that have a main function.
137
+
138
+ For issues related to config.h or extconf.rb, there was probably some
139
+ change to Discount's configure.sh that broke them. You will probably need
140
+ to update these files in ext/ manually.
141
+
142
+ For other errors, you'll have to investigate yourself. Common error classes:
143
+ * 'ext/configure.sh' fails:
144
+ - Create a patch to the upstream Discount.
145
+ * Some files missing from ext/ that are present in discount/:
146
+ - Update 'rake deploy' target to copy more files.
147
+ * Some files missing when `gem build` is run:
148
+ - Update gemspec to enumerate the correct files in ext/.
data/COPYING ADDED
@@ -0,0 +1,33 @@
1
+ The core Discount C sources are
2
+ Copyright (C) 2007 David Loren Parsons.
3
+
4
+ The Discount Ruby extension sources are
5
+ Copyright (C) 2008 Ryan Tomayko.
6
+
7
+ All rights reserved.
8
+
9
+ Redistribution and use in source and binary forms, with or without
10
+ modification, are permitted provided that the following conditions
11
+ are met:
12
+
13
+ 1. Redistributions of works must retain the original copyright notice,
14
+ this list of conditions and the following disclaimer.
15
+ 2. Redistributions in binary form must reproduce the original copyright
16
+ notice, this list of conditions and the following disclaimer in the
17
+ documentation and/or other materials provided with the distribution.
18
+ 3. Neither my name (David L Parsons) nor the names of contributors to
19
+ this code may be used to endorse or promote products derived
20
+ from this work without specific prior written permission.
21
+
22
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
+ POSSIBILITY OF SUCH DAMAGE.
data/README.markdown ADDED
@@ -0,0 +1,72 @@
1
+ Discount Markdown Processor for Ruby
2
+ ====================================
3
+ [![Build Status](https://travis-ci.org/davidfstr/rdiscount.png)](https://travis-ci.org/davidfstr/rdiscount)
4
+
5
+ Discount is an implementation of John Gruber's Markdown markup language in C. It
6
+ implements all of the language described in [the markdown syntax document][1] and
7
+ passes the [Markdown 1.0 test suite][2].
8
+
9
+ CODE: `git clone git://github.com/davidfstr/rdiscount.git`
10
+ HOME: <http://dafoster.net/projects/rdiscount/>
11
+ DOCS: <http://rdoc.info/github/davidfstr/rdiscount/master/RDiscount>
12
+ BUGS: <http://github.com/davidfstr/rdiscount/issues>
13
+
14
+ Discount was developed by [David Loren Parsons][3]. The Ruby extension
15
+ is maintained by [David Foster][4].
16
+
17
+ [1]: http://daringfireball.net/projects/markdown/syntax
18
+ [2]: http://daringfireball.net/projects/downloads/MarkdownTest_1.0.zip
19
+ [3]: http://www.pell.portland.or.us/~orc
20
+ [4]: https://github.com/davidfstr
21
+
22
+ INSTALL, HACKING
23
+ ----------------
24
+
25
+ New releases of RDiscount are published to [RubyGems][]:
26
+
27
+ $ [sudo] gem install rdiscount
28
+
29
+ The RDiscount sources are available via Git:
30
+
31
+ $ git clone git://github.com/davidfstr/rdiscount.git
32
+ $ cd rdiscount
33
+ $ rake --tasks
34
+
35
+ See the file [BUILDING][] for hacking instructions.
36
+
37
+ [RubyGems]: https://rubygems.org/gems/rdiscount
38
+ [BUILDING]: https://github.com/davidfstr/rdiscount/blob/master/BUILDING
39
+
40
+ USAGE
41
+ -----
42
+
43
+ RDiscount implements the basic protocol popularized by RedCloth and adopted
44
+ by BlueCloth:
45
+
46
+ require 'rdiscount'
47
+ markdown = RDiscount.new("Hello World!")
48
+ puts markdown.to_html
49
+
50
+ Additional processing options can be turned on when creating the
51
+ RDiscount object:
52
+
53
+ markdown = RDiscount.new("Hello World!", :smart, :filter_html)
54
+
55
+ Inject RDiscount into your BlueCloth-using code by replacing your bluecloth
56
+ require statements with the following:
57
+
58
+ begin
59
+ require 'rdiscount'
60
+ BlueCloth = RDiscount
61
+ rescue LoadError
62
+ require 'bluecloth'
63
+ end
64
+
65
+ COPYING
66
+ -------
67
+
68
+ Discount is free software; it is released under a BSD-style license
69
+ that allows you to do as you wish with it as long as you don't attempt
70
+ to claim it as your own work. RDiscount adopts Discount's license
71
+ verbatim. See the file `COPYING` for more information.
72
+
data/Rakefile ADDED
@@ -0,0 +1,224 @@
1
+ require 'date'
2
+ require 'rake/clean'
3
+ require 'digest/md5'
4
+
5
+ task :default => :test
6
+
7
+ # ==========================================================
8
+ # Ruby Extension
9
+ # ==========================================================
10
+
11
+ DLEXT = Config::MAKEFILE_CONFIG['DLEXT']
12
+ RUBYDIGEST = Digest::MD5.hexdigest(`ruby --version`)
13
+
14
+ file "ext/ruby-#{RUBYDIGEST}" do |f|
15
+ rm_f FileList["ext/ruby-*"]
16
+ touch f.name
17
+ end
18
+ CLEAN.include "ext/ruby-*"
19
+
20
+ file 'ext/Makefile' => FileList['ext/*.{c,h,rb}', "ext/ruby-#{RUBYDIGEST}"] do
21
+ chdir('ext') { ruby 'extconf.rb' }
22
+ end
23
+ CLEAN.include 'ext/Makefile', 'ext/mkmf.log'
24
+
25
+ file "ext/rdiscount.#{DLEXT}" => FileList["ext/Makefile"] do |f|
26
+ sh 'cd ext && make clean && make && rm -rf conftest.dSYM'
27
+ end
28
+ CLEAN.include 'ext/*.{o,bundle,so,dll}'
29
+
30
+ file "lib/rdiscount.#{DLEXT}" => "ext/rdiscount.#{DLEXT}" do |f|
31
+ cp f.prerequisites, "lib/", :preserve => true
32
+ end
33
+
34
+ desc 'Build the rdiscount extension'
35
+ task :build => "lib/rdiscount.#{DLEXT}"
36
+
37
+ # ==========================================================
38
+ # Manual
39
+ # ==========================================================
40
+
41
+ file 'man/rdiscount.1' => 'man/rdiscount.1.ronn' do
42
+ sh "ronn --manual=RUBY -b man/rdiscount.1.ronn"
43
+ end
44
+ CLOBBER.include 'man/rdiscount.1'
45
+
46
+ desc 'Build manpages'
47
+ task :man => 'man/rdiscount.1'
48
+
49
+ # ==========================================================
50
+ # Testing
51
+ # ==========================================================
52
+
53
+ require 'rake/testtask'
54
+ Rake::TestTask.new('test:unit') do |t|
55
+ t.test_files = FileList['test/*_test.rb']
56
+ t.ruby_opts += ['-rubygems'] if defined? Gem
57
+ end
58
+ task 'test:unit' => [:build]
59
+
60
+ desc 'Run conformance tests (MARKDOWN_TEST_VER=1.0)'
61
+ task 'test:conformance' => [:build] do |t|
62
+ script = "#{pwd}/bin/rdiscount"
63
+ test_version = ENV['MARKDOWN_TEST_VER'] || '1.0.3'
64
+ lib_dir = "#{pwd}/lib"
65
+ chdir("test/MarkdownTest_#{test_version}") do
66
+ result = `RUBYLIB=#{lib_dir} ./MarkdownTest.pl --script='#{script}' --tidy`
67
+ print result
68
+ fail unless result.include? "; 0 failed."
69
+ end
70
+ end
71
+
72
+ desc 'Run version 1.0 conformance suite'
73
+ task 'test:conformance:1.0' => [:build] do |t|
74
+ ENV['MARKDOWN_TEST_VER'] = '1.0'
75
+ Rake::Task['test:conformance'].invoke
76
+ end
77
+
78
+ desc 'Run 1.0.3 conformance suite'
79
+ task 'test:conformance:1.0.3' => [:build] do |t|
80
+ ENV['MARKDOWN_TEST_VER'] = '1.0.3'
81
+ Rake::Task['test:conformance'].invoke
82
+ end
83
+
84
+ desc 'Run unit and conformance tests'
85
+ task :test => %w[test:unit test:conformance]
86
+
87
+ desc 'Run benchmarks'
88
+ task :benchmark => :build do |t|
89
+ $:.unshift 'lib'
90
+ load 'test/benchmark.rb'
91
+ end
92
+
93
+ # ==========================================================
94
+ # Documentation
95
+ # ==========================================================
96
+
97
+ desc 'Generate API documentation'
98
+ task :doc => 'doc/index.html'
99
+
100
+ file 'doc/index.html' => FileList['lib/*.rb'] do |f|
101
+ sh((<<-end).gsub(/\s+/, ' '))
102
+ hanna --charset utf8 --fmt html --inline-source --line-numbers \
103
+ --main RDiscount --op doc --title 'RDiscount API Documentation' \
104
+ #{f.prerequisites.join(' ')}
105
+ end
106
+ end
107
+
108
+ CLEAN.include 'doc'
109
+
110
+ # ==========================================================
111
+ # Update package's Discount sources
112
+ # ==========================================================
113
+
114
+ desc 'Gather required discount sources into extension directory'
115
+ task :gather => 'discount/markdown.h' do |t|
116
+ # Files unique to /ext that should not be overridden
117
+ rdiscount_ext_files = [
118
+ "config.h",
119
+ "extconf.rb",
120
+ "rdiscount.c",
121
+ ]
122
+
123
+ # Files in /discount that have a main function and should not be copied to /ext
124
+ discount_c_files_with_main_function = [
125
+ "main.c",
126
+ "makepage.c",
127
+ "mkd2html.c",
128
+ "theme.c",
129
+ ]
130
+
131
+ # Ensure configure.sh was run
132
+ if not File.exists? 'discount/mkdio.h'
133
+ abort "discount/mkdio.h not found. Did you run ./configure.sh in the discount directory?"
134
+ end
135
+
136
+ # Delete all *.c and *.h files from ext that are not specific to RDiscount.
137
+ Dir.chdir("ext") do
138
+ c_files_to_delete = Dir["*.c"].select { |e| !rdiscount_ext_files.include? e }
139
+ h_files_to_delete = Dir["*.h"].select { |e| !rdiscount_ext_files.include? e }
140
+
141
+ rm (c_files_to_delete + h_files_to_delete),
142
+ :verbose => true
143
+ end
144
+
145
+ # Copy all *.c and *.h files from discount -> ext except those that
146
+ # RDiscount overrides. Also exclude Discount files with main functions.
147
+ Dir.chdir("discount") do
148
+ c_files_to_copy = Dir["*.c"].select { |e|
149
+ (!rdiscount_ext_files.include? e) &&
150
+ (!discount_c_files_with_main_function.include? e)
151
+ }
152
+ h_files_to_copy = Dir["*.h"].select { |e| !rdiscount_ext_files.include? e }
153
+
154
+ cp (c_files_to_copy + h_files_to_copy), '../ext/',
155
+ :preserve => true,
156
+ :verbose => true
157
+ end
158
+
159
+ # Copy special files from discount -> ext
160
+ cp 'discount/blocktags', 'ext/'
161
+ cp 'discount/VERSION', 'ext/'
162
+
163
+ # Copy man page
164
+ cp 'discount/markdown.7', 'man/'
165
+ end
166
+
167
+ file 'discount/markdown.h' do |t|
168
+ abort "The discount submodule is required. See the file BUILDING for getting set up."
169
+ end
170
+
171
+ # PACKAGING =================================================================
172
+
173
+ require 'rubygems'
174
+ $spec = eval(File.read('rdiscount.gemspec'))
175
+
176
+ def package(ext='')
177
+ "pkg/rdiscount-#{$spec.version}" + ext
178
+ end
179
+
180
+ desc 'Build packages'
181
+ task :package => %w[.gem .tar.gz].map {|e| package(e)}
182
+
183
+ desc 'Build and install as local gem'
184
+ task :install => package('.gem') do
185
+ sh "gem install #{package('.gem')}"
186
+ end
187
+
188
+ directory 'pkg/'
189
+
190
+ file package('.gem') => %w[pkg/ rdiscount.gemspec] + $spec.files do |f|
191
+ sh "gem build rdiscount.gemspec"
192
+ mv File.basename(f.name), f.name
193
+ end
194
+
195
+ file package('.tar.gz') => %w[pkg/] + $spec.files do |f|
196
+ sh "git archive --format=tar HEAD | gzip > #{f.name}"
197
+ end
198
+
199
+ # GEMSPEC HELPERS ==========================================================
200
+
201
+ def source_version
202
+ line = File.read('lib/rdiscount.rb')[/^\s*VERSION = .*/]
203
+ line.match(/.*VERSION = '(.*)'/)[1]
204
+ end
205
+
206
+ file 'rdiscount.gemspec' => FileList['Rakefile','lib/rdiscount.rb'] do |f|
207
+ # read spec file and split out manifest section
208
+ spec = File.read(f.name)
209
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
210
+ head.sub!(/\.version = '.*'/, ".version = '#{source_version}'")
211
+ head.sub!(/\.date = '.*'/, ".date = '#{Date.today.to_s}'")
212
+ # determine file list from git ls-files
213
+ files = `git ls-files`.
214
+ split("\n").
215
+ sort.
216
+ reject{ |file| file =~ /^\./ || file =~ /^test\/MarkdownTest/ }.
217
+ map{ |file| " #{file}" }.
218
+ join("\n")
219
+ # piece file back together and write...
220
+ manifest = " s.files = %w[\n#{files}\n ]\n"
221
+ spec = [head,manifest,tail].join(" # = MANIFEST =\n")
222
+ File.open(f.name, 'w') { |io| io.write(spec) }
223
+ puts "updated #{f.name}"
224
+ end
data/bin/rdiscount ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ # Usage: rdiscount [<file>...]
3
+ # Convert one or more Markdown files to HTML and write to standard output. With
4
+ # no <file> or when <file> is '-', read Markdown source text from standard input.
5
+ if ARGV.include?('--help')
6
+ File.read(__FILE__).split("\n").grep(/^# /).each do |line|
7
+ puts line[2..-1]
8
+ end
9
+ exit 0
10
+ end
11
+
12
+ require 'rdiscount'
13
+ STDOUT.write(RDiscount.new(ARGF.read).to_html)
data/ext/Csio.c ADDED
@@ -0,0 +1,61 @@
1
+ #include <stdio.h>
2
+ #include <string.h>
3
+ #include <stdarg.h>
4
+ #include "cstring.h"
5
+ #include "markdown.h"
6
+ #include "amalloc.h"
7
+
8
+
9
+ /* putc() into a cstring
10
+ */
11
+ void
12
+ Csputc(int c, Cstring *iot)
13
+ {
14
+ EXPAND(*iot) = c;
15
+ }
16
+
17
+
18
+ /* printf() into a cstring
19
+ */
20
+ int
21
+ Csprintf(Cstring *iot, char *fmt, ...)
22
+ {
23
+ va_list ptr;
24
+ int siz=100;
25
+
26
+ do {
27
+ RESERVE(*iot, siz);
28
+ va_start(ptr, fmt);
29
+ siz = vsnprintf(T(*iot)+S(*iot), ALLOCATED(*iot)-S(*iot), fmt, ptr);
30
+ va_end(ptr);
31
+ } while ( siz > (ALLOCATED(*iot)-S(*iot)) );
32
+
33
+ S(*iot) += siz;
34
+ return siz;
35
+ }
36
+
37
+
38
+ /* write() into a cstring
39
+ */
40
+ int
41
+ Cswrite(Cstring *iot, char *bfr, int size)
42
+ {
43
+ RESERVE(*iot, size);
44
+ memcpy(T(*iot)+S(*iot), bfr, size);
45
+ S(*iot) += size;
46
+ return size;
47
+ }
48
+
49
+
50
+ /* reparse() into a cstring
51
+ */
52
+ void
53
+ Csreparse(Cstring *iot, char *buf, int size, int flags)
54
+ {
55
+ MMIOT f;
56
+ ___mkd_initmmiot(&f, 0);
57
+ ___mkd_reparse(buf, size, 0, &f, 0);
58
+ ___mkd_emblock(&f);
59
+ SUFFIX(*iot, T(f.out), S(f.out));
60
+ ___mkd_freemmiot(&f, 0);
61
+ }
data/ext/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.1.7
data/ext/amalloc.c ADDED
@@ -0,0 +1,111 @@
1
+ /*
2
+ * debugging malloc()/realloc()/calloc()/free() that attempts
3
+ * to keep track of just what's been allocated today.
4
+ */
5
+
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+
9
+ #define MAGIC 0x1f2e3d4c
10
+
11
+ struct alist { int magic, size; struct alist *next, *last; };
12
+
13
+ static struct alist list = { 0, 0, 0, 0 };
14
+
15
+ static int mallocs=0;
16
+ static int reallocs=0;
17
+ static int frees=0;
18
+
19
+ void *
20
+ acalloc(int size, int count)
21
+ {
22
+ struct alist *ret = calloc(size + sizeof(struct alist), count);
23
+
24
+ if ( ret ) {
25
+ ret->magic = MAGIC;
26
+ ret->size = size * count;
27
+ if ( list.next ) {
28
+ ret->next = list.next;
29
+ ret->last = &list;
30
+ ret->next->last = ret;
31
+ list.next = ret;
32
+ }
33
+ else {
34
+ ret->last = ret->next = &list;
35
+ list.next = list.last = ret;
36
+ }
37
+ ++mallocs;
38
+ return ret+1;
39
+ }
40
+ return 0;
41
+ }
42
+
43
+
44
+ void*
45
+ amalloc(int size)
46
+ {
47
+ return acalloc(size,1);
48
+ }
49
+
50
+
51
+ void
52
+ afree(void *ptr)
53
+ {
54
+ struct alist *p2 = ((struct alist*)ptr)-1;
55
+
56
+ if ( p2->magic == MAGIC ) {
57
+ p2->last->next = p2->next;
58
+ p2->next->last = p2->last;
59
+ ++frees;
60
+ free(p2);
61
+ }
62
+ else
63
+ free(ptr);
64
+ }
65
+
66
+
67
+ void *
68
+ arealloc(void *ptr, int size)
69
+ {
70
+ struct alist *p2 = ((struct alist*)ptr)-1;
71
+ struct alist save;
72
+
73
+ if ( p2->magic == MAGIC ) {
74
+ save.next = p2->next;
75
+ save.last = p2->last;
76
+ p2 = realloc(p2, sizeof(*p2) + size);
77
+
78
+ if ( p2 ) {
79
+ p2->size = size;
80
+ p2->next->last = p2;
81
+ p2->last->next = p2;
82
+ ++reallocs;
83
+ return p2+1;
84
+ }
85
+ else {
86
+ save.next->last = save.last;
87
+ save.last->next = save.next;
88
+ return 0;
89
+ }
90
+ }
91
+ return realloc(ptr, size);
92
+ }
93
+
94
+
95
+ void
96
+ adump()
97
+ {
98
+ struct alist *p;
99
+
100
+
101
+ for ( p = list.next; p && (p != &list); p = p->next ) {
102
+ fprintf(stderr, "allocated: %d byte%s\n", p->size, (p->size==1) ? "" : "s");
103
+ fprintf(stderr, " [%.*s]\n", p->size, (char*)(p+1));
104
+ }
105
+
106
+ if ( getenv("AMALLOC_STATISTICS") ) {
107
+ fprintf(stderr, "%d malloc%s\n", mallocs, (mallocs==1)?"":"s");
108
+ fprintf(stderr, "%d realloc%s\n", reallocs, (reallocs==1)?"":"s");
109
+ fprintf(stderr, "%d free%s\n", frees, (frees==1)?"":"s");
110
+ }
111
+ }