1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
glib: Portability and Utility
</title>
<meta name="GENERATOR" content=
"Modular DocBook HTML Stylesheet Version 1.45">
<link rel="HOME" title="GTK+ / Gnome Application Development"
href="ggad.html">
<link rel="UP" title="Overview" href="overview.html">
<link rel="PREVIOUS" title="Structure of the Book" href=
"z22.html">
<link rel="NEXT" title="Data Structures" href="z29.html">
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=
"#840084" alink="#0000FF">
<div class="NAVHEADER">
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<th colspan="4" align="center">
<font color="#000000" size="2">GTK+ / Gnome Application
Development</font>
</th>
</tr>
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="z22.html"><font color="#0000ff" size="2"><b>
<<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="z29.html"><font color="#0000ff" size="2"><b>
Next >>></b></font></a>
</td>
</tr>
</table>
</div>
<div class="CHAPTER">
<h1>
<a name="CHA-GLIB">glib: Portability and Utility</a>
</h1>
<div class="TOC">
<dl>
<dt>
<b>Table of Contents</b>
</dt>
<dt>
<a href="cha-glib.html#Z24">Basics</a>
</dt>
<dt>
<a href="z29.html">Data Structures</a>
</dt>
<dt>
<a href="z35.html">Other Features</a>
</dt>
</dl>
</div>
<p>
glib is a C portability and utility library for UNIX-like
systems and Windows. This chapter covers some of the most
commonly-used library features in GTK+ and Gnome
applications. glib is simple, and the concepts are
familiar; so we'll move quickly. For more complete coverage
of glib, see <tt class="FILENAME">glib.h</tt> or the free
glib reference manual that comes with the library. (By the
way: don't be afraid of using the glib, GTK+, or Gnome
header files; they are very clean and easy to read, and are
handy as a quick reference. For that matter, don't be
afraid to look at the source code, if you have very
specific questions about the implementation.)
</p>
<p>
glib's various facilities are intended to have a consistent
interface; the coding style is semi-object-oriented, and
identifiers are prefixed with "g" to create a kind of
namespace.
</p>
<p>
glib has a single header file, <tt class="FILENAME">
glib.h</tt>.
</p>
<div class="SECT1">
<h1 class="SECT1">
<a name="Z24">Basics</a>
</h1>
<p>
glib provides substitutes for many standard and
commonly-used C language constructs. This section
describes glib's fundamental type definitions, macros,
memory allocation routines, and string utility functions.
</p>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z25">Type Definitions</a>
</h2>
<p>
Rather than using C's standard types (<span class=
"STRUCTNAME">int</span>, <span class="STRUCTNAME">
long</span>, etc.) glib defines its own. These serve a
variety of purposes. For example, <span class=
"STRUCTNAME">gint32</span> is guaranteed to be 32 bits
wide, something no standard C type can ensure. <span
class="STRUCTNAME">guint</span> is simply easier to
type than <span class="STRUCTNAME">unsigned</span>. A
few of the typedefs exist only for consistency; for
example, <span class="STRUCTNAME">gchar</span> is
always equivalent to the standard <span class=
"STRUCTNAME">char</span>.
</p>
<p>
The following primitive types are defined by glib:
</p>
<ul>
<li>
<p>
<span class="STRUCTNAME">gint8</span>, <span class=
"STRUCTNAME">guint8</span>, <span class=
"STRUCTNAME">gint16</span>, <span class=
"STRUCTNAME">guint16</span>, <span class=
"STRUCTNAME">gint32</span>, <span class=
"STRUCTNAME">guint32</span>, <span class=
"STRUCTNAME">gint64</span>, <span class=
"STRUCTNAME">guint64</span>---these give you
integers of a guaranteed size. Not all platforms
provide 64-bit integers; if a platform has them,
glib will define <tt class="FUNCTION">
G_HAVE_GINT64</tt>. (If it isn't obvious, the <span
class="STRUCTNAME">guint</span> types are unsigned,
the <span class="STRUCTNAME">gint</span> types are
signed.)
</p>
</li>
<li>
<p>
<span class="STRUCTNAME">gboolean</span> is useful
to make your code more readable, since C has no
<span class="STRUCTNAME">bool</span> type.
</p>
</li>
<li>
<p>
<span class="STRUCTNAME">gchar</span>, <span class=
"STRUCTNAME">gshort</span>, <span class=
"STRUCTNAME">glong</span>, <span class=
"STRUCTNAME">gint</span>, <span class="STRUCTNAME">
gfloat</span>, <span class="STRUCTNAME">
gdouble</span> are purely cosmetic.
</p>
</li>
<li>
<p>
<span class="STRUCTNAME">gpointer</span> may be
more convenient to type than <span class=
"STRUCTNAME">void *</span>. <span class=
"STRUCTNAME">gconstpointer</span> gives you <span
class="STRUCTNAME">const void*</span>. (<span
class="STRUCTNAME">const gpointer</span> will <i
class="EMPHASIS">not</i> do what you typically mean
it to; spend some time with a good book on C if you
don't see why.)
</p>
</li>
</ul>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z26">Frequently Used Macros</a>
</h2>
<p>
glib defines a number of familiar macros used in many C
programs, shown in <a href=
"cha-glib.html#ML-SIMPLEMACROS">Figure 1</a>. All of
these should be self-explanatory. <tt class="FUNCTION">
MIN()</tt>/<tt class="FUNCTION">MAX()</tt> return the
smaller or larger of their arguments. <tt class=
"FUNCTION">ABS()</tt> returns the absolute value of its
argument. <tt class="FUNCTION">CLAMP(x, low, high)</tt>
means <span class="STRUCTNAME">x</span>, unless <span
class="STRUCTNAME">x</span> is outside the range [<span
class="STRUCTNAME">low</span>, <span class=
"STRUCTNAME">high</span>]; if <span class="STRUCTNAME">
x</span> is below the range, <span class="STRUCTNAME">
low</span> is returned; if <span class="STRUCTNAME">
x</span> is above the range, <span class="STRUCTNAME">
high</span> is returned. In addition to the macros
shown in <a href="cha-glib.html#ML-SIMPLEMACROS">Figure
1</a>, <tt class="FUNCTION">TRUE</tt>/<tt class=
"FUNCTION">FALSE</tt>/<tt class="FUNCTION">NULL</tt>
are defined as the usual <span class="STRUCTNAME">
1</span>/<span class="STRUCTNAME">0</span>/<span class=
"STRUCTNAME">((void*)0)</span>.
</p>
<div class="FIGURE">
<a name="ML-SIMPLEMACROS"></a>
<div class="FUNCSYNOPSIS">
<a name="ML-SIMPLEMACROS.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
MAX</tt></code>(<tt class=
"PARAMETER"><i>a</i></tt>, <tt class="PARAMETER">
<i>b</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
MIN</tt></code>(<tt class=
"PARAMETER"><i>a</i></tt>, <tt class="PARAMETER">
<i>b</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
ABS</tt></code>(<tt class=
"PARAMETER"><i>x</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
CLAMP</tt></code>(<tt class=
"PARAMETER"><i>x</i></tt>, <tt class="PARAMETER">
<i>low</i></tt>, <tt class="PARAMETER"><i>
high</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 1. Familiar C Macros</b>
</p>
</div>
<p>
There are also many macros unique to glib, such as the
portable <span class="STRUCTNAME">
gpointer</span>-to-<span class="STRUCTNAME">gint</span>
and <span class="STRUCTNAME">gpointer</span>-to-<span
class="STRUCTNAME">guint</span> conversions shown in <a
href="cha-glib.html#ML-POINTERINT">Figure 2</a>.
</p>
<p>
Most of glib's data structures are designed to store a
<span class="STRUCTNAME">gpointer</span>. If you want
to store pointers to dynamically allocated objects,
this is the right thing. However, sometimes you want to
store a simple list of integers without having to
dynamically allocate them. Though the C standard does
not strictly guarantee it, it is possible to store a
<span class="STRUCTNAME">gint</span> or <span class=
"STRUCTNAME">guint</span> in a <span class=
"STRUCTNAME">gpointer</span> variable on the wide range
of platforms glib has been ported to; in some cases, an
intermediate cast is required. The macros in <a href=
"cha-glib.html#ML-POINTERINT">Figure 2</a> abstract the
presence of the cast.
</p>
<p>
Here's an example:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gint my_int;
gpointer my_pointer;
my_int = 5;
my_pointer = GINT_TO_POINTER(my_int);
printf("We are storing %d\n", GPOINTER_TO_INT(my_pointer));
</pre>
</td>
</tr>
</table>
<p>
Be careful, though; these macros allow you to store an
integer in a pointer, but storing a pointer in an
integer will <i class="EMPHASIS">not</i> work. To do
that portably, you must store the pointer in a <span
class="STRUCTNAME">long</span>. (It's undoubtedly a bad
idea to do so, however.)
</p>
<div class="FIGURE">
<a name="ML-POINTERINT"></a>
<div class="FUNCSYNOPSIS">
<a name="ML-POINTERINT.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
GINT_TO_POINTER</tt></code>(<tt class=
"PARAMETER"><i>p</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
GPOINTER_TO_INT</tt></code>(<tt class=
"PARAMETER"><i>p</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
GUINT_TO_POINTER</tt></code>(<tt class=
"PARAMETER"><i>p</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
GPOINTER_TO_UINT</tt></code>(<tt class=
"PARAMETER"><i>p</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 2. Macros for storing integers in
pointers</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z27">Debugging Macros</a>
</h2>
<p>
glib has a nice set of macros you can use to enforce
invariants and preconditions in your code. GTK+ uses
these liberally---one of the reasons it's so stable and
easy to use. They all disappear when you define <tt
class="FUNCTION">G_DISABLE_CHECKS</tt> or <tt class=
"FUNCTION">G_DISABLE_ASSERT</tt>, so there's no
performance penalty in production code. Using these
liberally is a very, very good idea. You'll find bugs
much faster if you do. You can even add assertions and
checks whenever you find a bug to be sure the bug
doesn't reappear in future versions---this complements
a regression suite. Checks are especially useful when
the code you're writing will be used as a black box by
other programmers; users will immediately know when and
how they've misused your code.
</p>
<p>
Of course you should be very careful to ensure your
code isn't subtly dependent on debug-only statements to
function correctly. Statements that will disappear in
production code should <i class="EMPHASIS">never</i>
have side effects.
</p>
<div class="FIGURE">
<a name="ML-PRECONDITION"></a>
<div class="FUNCSYNOPSIS">
<a name="ML-PRECONDITION.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
g_return_if_fail</tt></code>(<tt class=
"PARAMETER"><i>condition</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
g_return_val_if_fail</tt></code>(<tt class=
"PARAMETER"><i>condition</i></tt>, <tt class=
"PARAMETER"><i>retval</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 3. Precondition Checks</b>
</p>
</div>
<p>
<a href="cha-glib.html#ML-PRECONDITION">Figure 3</a>
shows glib's precondition checks. <tt class="FUNCTION">
g_return_if_fail()</tt> prints a warning and
immediately returns from the current function if <span
class="STRUCTNAME">condition</span> is <span class=
"STRUCTNAME">FALSE</span>. <tt class="FUNCTION">
g_return_val_if_fail()</tt> is similar but allows you
to return some <span class="STRUCTNAME">retval</span>.
These macros are incredibly useful---if you use them
liberally, especially in combination with GTK+'s
runtime type checking, you'll halve the time you spend
looking for bad pointers and type errors.
</p>
<p>
Using these functions is simple; here's an example from
the glib hash table implementation:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
void
g_hash_table_foreach (GHashTable *hash_table,
GHFunc func,
gpointer user_data)
{
GHashNode *node;
gint i;
g_return_if_fail (hash_table != NULL);
g_return_if_fail (func != NULL);
for (i = 0; i < hash_table->size; i++)
for (node = hash_table->nodes[i]; node; node = node->next)
(* func) (node->key, node->value, user_data);
}
</pre>
</td>
</tr>
</table>
<p>
Without the checks, passing <span class="STRUCTNAME">
NULL</span> as a parameter to this function would
result in a mysterious segmentation fault. The person
using the library would have to figure out where the
error occurred with a debugger, and maybe even dig in
to the glib code to see what was wrong. With the
checks, they'll get a nice error message telling them
that <span class="STRUCTNAME">NULL</span> arguments are
not allowed.
</p>
<div class="FIGURE">
<a name="ML-ASSERTIONS"></a>
<div class="FUNCSYNOPSIS">
<a name="ML-ASSERTIONS.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
g_assert</tt></code>(<tt class=
"PARAMETER"><i>condition</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
g_assert_not_reached</tt></code>(void);</code>
</p>
</div>
<p>
<b>Figure 4. Assertions</b>
</p>
</div>
<p>
glib also has more traditional assertion macros, shown
in <a href="cha-glib.html#ML-ASSERTIONS">Figure 4</a>.
<tt class="FUNCTION">g_assert()</tt> is basically
identical to <tt class="FUNCTION">assert()</tt>, but
responds to <span class="STRUCTNAME">
G_DISABLE_ASSERT</span> and behaves consistently across
all platforms. <tt class="FUNCTION">
g_assert_not_reached()</tt> is also provided; this is
an assertion which always fails. Assertions call <tt
class="FUNCTION">abort()</tt> to exit the program and
(if your environment supports it) dump a core file for
debugging purposes.
</p>
<p>
Fatal assertions should be used to check <i class=
"EMPHASIS">internal consistency</i> of a function or
library, while <tt class="FUNCTION">
g_return_if_fail()</tt> is intended to ensure sane
values are passed to the public interfaces of a program
module. That is, if an assertion fails, you typically
look for a bug in the module containing the assertion;
if a <tt class="FUNCTION">g_return_if_fail()</tt> check
fails, you typically look for the bug in the code which
invokes the module.
</p>
<p>
This code from glib's calendrical calculations module
shows the difference:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
GDate*
g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
{
GDate *d;
g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
d = g_new (GDate, 1);
d->julian = FALSE;
d->dmy = TRUE;
d->month = m;
d->day = day;
d->year = y;
g_assert (g_date_valid (d));
return d;
}
</pre>
</td>
</tr>
</table>
<p>
The precondition check at the beginning ensures the
user passes in reasonable values for the day, month and
year; the assertion at the end ensures that glib
constructed a sane object, given sane values.
</p>
<p>
<tt class="FUNCTION">g_assert_not_reached()</tt> should
be used to mark "impossible" situations; a common use
is to detect switch statements that don't handle all
possible values of an enumeration:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
switch (val)
{
case FOO_ONE:
break;
case FOO_TWO:
break;
default:
/* Invalid enumeration value */
g_assert_not_reached();
break;
}
</pre>
</td>
</tr>
</table>
<p>
All of the debugging macros print a warning using
glib's <tt class="FUNCTION">g_log()</tt> facility,
which means the warning includes the name of the
originating application or library, and you can
optionally install a replacement warning-printing
routine. For example, you might send all warnings to a
dialog box or log file instead of printing them on the
console.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="GLIB-MEMORY">Memory</a>
</h2>
<p>
glib wraps the standard <tt class="FUNCTION">
malloc()</tt> and <tt class="FUNCTION">free()</tt> with
its own <span class="STRUCTNAME">g_</span> variants,
<tt class="FUNCTION">g_malloc()</tt> and <tt class=
"FUNCTION">g_free()</tt>, shown in <a href=
"cha-glib.html#FL-MEMORY">Figure 5</a>. These are nice
in several small ways:
</p>
<ul>
<li>
<p>
<tt class="FUNCTION">g_malloc()</tt> always returns
a <span class="STRUCTNAME">gpointer</span>, never a
<span class="STRUCTNAME">char*</span>, so there's
no need to cast the return value.
</p>
</li>
<li>
<p>
<tt class="FUNCTION">g_malloc()</tt> aborts the
program if the underlying <tt class="FUNCTION">
malloc()</tt> fails, so you don't have to check for
a <span class="STRUCTNAME">NULL</span> return
value.
</p>
</li>
<li>
<p>
<tt class="FUNCTION">g_malloc()</tt> gracefully
handles a <span class="STRUCTNAME">size</span> of
<span class="STRUCTNAME">0</span>, by returning
<span class="STRUCTNAME">NULL</span>.
</p>
</li>
<li>
<p>
<tt class="FUNCTION">g_free()</tt> will ignore any
<span class="STRUCTNAME">NULL</span> pointers you
pass to it.
</p>
</li>
</ul>
<p>
In addition to these minor conveniences, <tt class=
"FUNCTION">g_malloc()</tt> and <tt class="FUNCTION">
g_free()</tt> can support various kinds of memory
debugging and profiling. If you pass the <tt class=
"APPLICATION">--enable-mem-check</tt> option to glib's
configure script, the compiled <tt class="FUNCTION">
g_free()</tt> will warn you whenever you free the same
pointer twice. The <tt class="APPLICATION">
--enable-mem-profile</tt> option enables code which
keeps memory use statistics; when you call <tt class=
"FUNCTION">g_mem_profile()</tt> they are printed to the
console. Finally, you can define <span class=
"STRUCTNAME">USE_DMALLOC</span> and the glib memory
wrappers will use the <span class="STRUCTNAME">
MALLOC()</span>, etc. debugging macros available in <tt
class="FILENAME">dmalloc.h</tt> on some platforms.
</p>
<div class="FIGURE">
<a name="FL-MEMORY"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-MEMORY.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">gpointer <tt class=
"FUNCTION">g_malloc</tt></code>(gulong <tt class=
"PARAMETER"><i>size</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">g_free</tt></code>(gpointer <tt class=
"PARAMETER"><i>mem</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gpointer <tt class=
"FUNCTION">g_realloc</tt></code>(gpointer <tt
class="PARAMETER"><i>mem</i></tt>, gulong <tt
class="PARAMETER"><i>size</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gpointer <tt class=
"FUNCTION">g_memdup</tt></code>(gconstpointer <tt
class="PARAMETER"><i>mem</i></tt>, guint <tt class=
"PARAMETER"><i>bytesize</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 5. glib memory allocation</b>
</p>
</div>
<p>
It's important to match <tt class="FUNCTION">
g_malloc()</tt> with <tt class="FUNCTION">
g_free()</tt>, plain <tt class="FUNCTION">malloc()</tt>
with <tt class="FUNCTION">free()</tt>, and (if you're
using C++) <span class="STRUCTNAME">new</span> with
<span class="STRUCTNAME">delete</span>. Otherwise bad
things can happen, since these allocators may use
different memory pools (and <span class="STRUCTNAME">
new</span>/<span class="STRUCTNAME">delete</span> call
constructors and destructors).
</p>
<p>
Of course there's a <tt class="FUNCTION">
g_realloc()</tt> equivalent to <tt class="FUNCTION">
realloc()</tt>. There's also a convenient <tt class=
"FUNCTION">g_malloc0()</tt> which fills allocated
memory with 0s, and <tt class="FUNCTION">
g_memdup()</tt> which returns a copy of <span class=
"STRUCTNAME">bytesize</span> bytes starting at <span
class="STRUCTNAME">mem</span>. <tt class="FUNCTION">
g_realloc()</tt> and <tt class="FUNCTION">
g_malloc0()</tt> will both accept a <span class=
"STRUCTNAME">size</span> of <span class="STRUCTNAME">
0</span>, for consistency with <span class=
"STRUCTNAME">g_malloc()</span>. However, <tt class=
"FUNCTION">g_memdup()</tt> will not.
</p>
<p>
If it isn't obvious: <tt class="FUNCTION">
g_malloc0()</tt> fills raw memory with unset bits, not
the value <span class="STRUCTNAME">0</span> for
whatever type you intend to put there. Occasionally
someone expects to get an array of floating point
numbers initialized to <span class="STRUCTNAME">
0.0</span>; this will <i class="EMPHASIS">not</i> work.
</p>
<p>
Finally, there are type-aware allocation macros, shown
in <a href="cha-glib.html#ML-G-NEW">Figure 6</a>. The
<span class="STRUCTNAME">type</span> argument to each
of these is the name of a type, and the <span class=
"STRUCTNAME">count</span> argument is the number of
<span class="STRUCTNAME">type</span>-size blocks to
allocate. These macros save you some typing and
multiplication, and are thus less error-prone. They
automatically cast to the target pointer type, so
attempting to assign the allocated memory to the wrong
kind of pointer should trigger a compiler warning. (If
you have warnings turned on, as a responsible
programmer should!)
</p>
<div class="FIGURE">
<a name="ML-G-NEW"></a>
<div class="FUNCSYNOPSIS">
<a name="ML-G-NEW.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
g_new</tt></code>(<tt class=
"PARAMETER"><i>type</i></tt>, <tt class=
"PARAMETER"><i>count</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
g_new0</tt></code>(<tt class=
"PARAMETER"><i>type</i></tt>, <tt class=
"PARAMETER"><i>count</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF"><tt class="FUNCTION">
g_renew</tt></code>(<tt class=
"PARAMETER"><i>type</i></tt>, <tt class=
"PARAMETER"><i>mem</i></tt>, <tt class="PARAMETER">
<i>count</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 6. Allocation macros</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z28">String Handling</a>
</h2>
<p>
glib provides a number of functions for string
handling; some are unique to glib, and some solve
portability concerns. They all interoperate nicely with
the glib memory allocation routines.
</p>
<p>
For those interested in a better string than <span
class="STRUCTNAME">gchar*</span>, there's also a <span
class="STRUCTNAME">GString</span> type. It isn't
covered in this book, but documentation is available at
<a href="http://www.gtk.org/" target="_top">
http://www.gtk.org/</a>.
</p>
<div class="FIGURE">
<a name="FL-STREXT"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-STREXT.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">gint <tt class=
"FUNCTION">g_snprintf</tt></code>(gchar* <tt class=
"PARAMETER"><i>buf</i></tt>, gulong <tt class=
"PARAMETER"><i>n</i></tt>, const gchar* <tt class=
"PARAMETER"><i>format</i></tt>, <tt class=
"PARAMETER"><i>...</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gint <tt class=
"FUNCTION">g_strcasecmp</tt></code>(const gchar*
<tt class="PARAMETER"><i>s1</i></tt>, const gchar*
<tt class="PARAMETER"><i>s2</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gint <tt class=
"FUNCTION">g_strncasecmp</tt></code>(const gchar*
<tt class="PARAMETER"><i>s1</i></tt>, const gchar*
<tt class="PARAMETER"><i>s2</i></tt>, guint <tt
class="PARAMETER"><i>n</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 7. Portability Wrappers</b>
</p>
</div>
<p>
<a href="cha-glib.html#FL-STREXT">Figure 7</a> shows
some substitutes glib provides for commonly-implemented
but unportable extensions to ANSI C.
</p>
<p>
One of the annoying things about C is that it provides
the crash-causing, security-hole-creating, generally
evil <tt class="FUNCTION">sprintf()</tt>, but the
relatively safe and widely implemented <tt class=
"FUNCTION">snprintf()</tt> is a vendor extension. <tt
class="FUNCTION">g_snprintf()</tt> wraps native <tt
class="FUNCTION">snprintf()</tt> on platforms that have
it, and provides an implementation on those that don't.
So you can say goodbye to <tt class="FUNCTION">
sprintf()</tt> forever. Even better: classically, <tt
class="FUNCTION">snprintf()</tt> does not guarantee
that it will <span class="STRUCTNAME">
NULL</span>-terminate the buffer it fills. <tt class=
"FUNCTION">g_snprintf()</tt> does.
</p>
<p>
<tt class="FUNCTION">g_strcasecmp()</tt> and <tt class=
"FUNCTION">g_strncasecmp()</tt> perform a
case-insensitive comparison of two strings, optionally
with a maximum length. <span class="STRUCTNAME">
strcasecmp()</span> is available on many platforms but
not universally, so using glib instead is advisable.
</p>
<p>
The functions in <a href="cha-glib.html#FL-STRMANIP">
Figure 8</a> modify a string in-place: the first two
convert the string to lowercase or uppercase,
respectively, while <tt class="FUNCTION">
g_strreverse()</tt> reverses its characters. <tt class=
"FUNCTION">g_strchug()</tt> and <tt class="FUNCTION">
g_strchomp()</tt> "chug" the string (remove leading
spaces), or "chomp" it (remove trailing spaces). These
last two return the string, in addition to modifying it
in-place; in some cases it may be convenient to use the
return value. There is a macro, <tt class="FUNCTION">
g_strstrip()</tt>, which combines both functions to
remove both leading and trailing spaces; it is used
just as the individual functions are.
</p>
<div class="FIGURE">
<a name="FL-STRMANIP"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-STRMANIP.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">g_strdown</tt></code>(gchar* <tt class=
"PARAMETER"><i>string</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">g_strup</tt></code>(gchar* <tt class=
"PARAMETER"><i>string</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">g_strreverse</tt></code>(gchar* <tt
class="PARAMETER"><i>string</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strchug</tt></code>(gchar* <tt class=
"PARAMETER"><i>string</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strchomp</tt></code>(gchar* <tt class=
"PARAMETER"><i>string</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 8. In-place string modifications</b>
</p>
</div>
<p>
<a href="cha-glib.html#FL-STRFORMATS">Figure 9</a>
shows a few more semi-standard functions glib wraps.
<tt class="FUNCTION">g_strtod</tt> is like <tt class=
"FUNCTION">strtod()</tt>---it converts string <span
class="STRUCTNAME">nptr</span> to a double---with the
exception that it will also attempt to convert the
double in the <tt class="APPLICATION">"C"</tt> locale
if it fails to convert it in the user's default locale.
<span class="STRUCTNAME">*endptr</span> is set to the
first unconverted character, i.e. any text after the
number representation. If conversion fails, <span
class="STRUCTNAME">*endptr</span> is set to <span
class="STRUCTNAME">nptr</span>. <span class=
"STRUCTNAME">endptr</span> may be <span class=
"STRUCTNAME">NULL</span>, causing it to be ignored.
</p>
<p>
<tt class="FUNCTION">g_strerror()</tt> and <tt class=
"FUNCTION">g_strsignal()</tt> are like their non-<span
class="STRUCTNAME">g_</span> equivalents, but portable.
(They return a string representation for an <span
class="STRUCTNAME">errno</span> or a signal number.)
</p>
<div class="FIGURE">
<a name="FL-STRFORMATS"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-STRFORMATS.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">gdouble <tt class=
"FUNCTION">g_strtod</tt></code>(const gchar* <tt
class="PARAMETER"><i>nptr</i></tt>, gchar** <tt
class="PARAMETER"><i>endptr</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strerror</tt></code>(gint <tt class=
"PARAMETER"><i>errnum</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strsignal</tt></code>(gint <tt class=
"PARAMETER"><i>signum</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 9. String Conversions</b>
</p>
</div>
<p>
<a href="cha-glib.html#FL-STRDUP">Figure 10</a> shows
glib's rich array of functions for allocating strings.
Unsurprisingly, <tt class="FUNCTION">g_strdup()</tt>
and <tt class="FUNCTION">g_strndup()</tt> produce an
allocated copy of <span class="STRUCTNAME">str</span>
or the first <span class="STRUCTNAME">n</span>
characters of <span class="STRUCTNAME">str</span>. For
consistency with the glib memory allocation functions,
they return <span class="STRUCTNAME">NULL</span> if
passed a <span class="STRUCTNAME">NULL</span> pointer.
The <tt class="FUNCTION">printf()</tt> variants return
a formatted string. <tt class="FUNCTION">
g_strescape</tt> escapes any <span class="STRUCTNAME">
\</span> characters in its argument by inserting
another <span class="STRUCTNAME">\</span> before them,
returning the escaped string. <tt class="FUNCTION">
g_strnfill()</tt>returns a string of size <span class=
"STRUCTNAME">length</span> filled with <span class=
"STRUCTNAME">fill_char</span>.
</p>
<p>
<tt class="FUNCTION">g_strdup_printf()</tt> deserves a
special mention; it is a simpler way to handle this
common piece of code:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gchar* str = g_malloc(256);
g_snprintf(str, 256, "%d printf-style %s", 1, "format");
</pre>
</td>
</tr>
</table>
<p>
Instead you could say this, and avoid having to figure
out the proper length of the buffer to boot:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gchar* str = g_strdup_printf("%d printf-style %s", 1, "format");
</pre>
</td>
</tr>
</table>
<div class="FIGURE">
<a name="FL-STRDUP"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-STRDUP.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strdup</tt></code>(const gchar* <tt
class="PARAMETER"><i>str</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strndup</tt></code>(const gchar* <tt
class="PARAMETER"><i>format</i></tt>, guint <tt
class="PARAMETER"><i>n</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strdup_printf</tt></code>(const gchar*
<tt class="PARAMETER"><i>format</i></tt>, <tt
class="PARAMETER"><i>...</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strdup_vprintf</tt></code>(const
gchar* <tt class="PARAMETER"><i>format</i></tt>,
va_list <tt class="PARAMETER"><i>
args</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strescape</tt></code>(gchar* <tt
class="PARAMETER"><i>string</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strnfill</tt></code>(guint <tt class=
"PARAMETER"><i>length</i></tt>, gchar <tt class=
"PARAMETER"><i>fill_char</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 10. Allocating Strings</b>
</p>
</div>
<p>
glib provides some convenient functions for
concatenating strings, shown in <a href=
"cha-glib.html#FL-STRCONCAT">Figure 11</a>. <tt class=
"FUNCTION">g_strconcat()</tt> returns a newly-allocated
string created by concatenating each of the strings in
the argument list. The last argument must be <span
class="STRUCTNAME">NULL</span>, so <tt class=
"FUNCTION">g_strconcat()</tt> knows when to stop. <tt
class="FUNCTION">g_strjoin()</tt> is similar, but <span
class="STRUCTNAME">separator</span> is inserted between
each string. If <span class="STRUCTNAME">
separator</span> is <span class="STRUCTNAME">
NULL</span>, no separator is used.
</p>
<div class="FIGURE">
<a name="FL-STRCONCAT"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-STRCONCAT.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strconcat</tt></code>(const gchar* <tt
class="PARAMETER"><i>string1</i></tt>, <tt class=
"PARAMETER"><i>...</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strjoin</tt></code>(const gchar* <tt
class="PARAMETER"><i>separator</i></tt>, <tt class=
"PARAMETER"><i>...</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 11. Concatenating strings</b>
</p>
</div>
<p>
Finally, <a href="cha-glib.html#FL-STRVECTOR">Figure
12</a> summarizes a few routines which manipulate <span
class="STRUCTNAME">NULL</span>-terminated arrays of
strings. <tt class="FUNCTION">g_strsplit()</tt> breaks
<span class="STRUCTNAME">string</span> at each <span
class="STRUCTNAME">delimiter</span>, returning a
newly-allocated array. <tt class="FUNCTION">
g_strjoinv()</tt> concatenates each string in the array
with an optional <span class="STRUCTNAME">
separator</span>, returning an allocated string. <tt
class="FUNCTION">g_strfreev()</tt> frees each string in
the array and then the array itself.
</p>
<div class="FIGURE">
<a name="FL-STRVECTOR"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-STRVECTOR.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">gchar** <tt class=
"FUNCTION">g_strsplit</tt></code>(const gchar* <tt
class="PARAMETER"><i>string</i></tt>, const gchar*
<tt class="PARAMETER"><i>delimiter</i></tt>, gint
<tt class="PARAMETER"><i>
max_tokens</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">g_strjoinv</tt></code>(const gchar* <tt
class="PARAMETER"><i>separator</i></tt>, gchar**
<tt class="PARAMETER"><i>
str_array</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">g_strfreev</tt></code>(gchar** <tt
class="PARAMETER"><i>str_array</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 12. Manipulating <span class="STRUCTNAME">
NULL</span>-terminated string vectors</b>
</p>
</div>
</div>
</div>
</div>
<div class="NAVFOOTER">
<br>
<br>
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="z22.html"><font color="#0000ff" size="2"><b>
<<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="z29.html"><font color="#0000ff" size="2"><b>
Next >>></b></font></a>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<font color="#000000" size="2"><b>Structure of the
Book</b></font>
</td>
<td colspan="2" align="right">
<font color="#000000" size="2"><b>Data
Structures</b></font>
</td>
</tr>
</table>
</div>
</body>
</html>
|